c# - DateTime 在使用 Refit 序列化并用作 url 参数时失去精度

标签 c# asp.net-core refit

我正在构建一个 API 网关,它有一个将 DateTime 值作为参数的端点。它使用 Refit 将此查询转发到底层微服务。

我的问题是,当构建微服务查询的 URL 时,DateTime 值失去了精度。

有没有办法将 Refit 配置为在构建 URL 时使用自定义 DateTime 序列化程序?

微服务端点定义如下:

[Get("/client-sales")]
Task<ClientSaleCollectionR12n> SearchClientSales([Header("Authorization")] string authorization,
                                                         DateTime? completedAfter = null,
                                                         DateTime? completedBefore = null);

发送到网关的查询:

GET /client-sales?completedAfter=2020-03-20T14:54:26.786&completedBefore=2020-03-21T15:16:33.212

转发到底层微服务时变成这样:

GET /client-sales?completedAfter=03%2F20%2F2020%2014%3A54%3A26&completedBefore=03%2F21%2F2020%2015%3A16%3A33

最佳答案

我能够在定义接口(interface)时指定格式。

Task<ClientSaleCollectionR12n> SearchClientSales([Header("Authorization")] string authorization,
                                                         [Query(Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")] DateTime? completedAfter = null,
                                                         [Query(Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")] DateTime? completedBefore = null);

关于c# - DateTime 在使用 Refit 序列化并用作 url 参数时失去精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60810112/

相关文章:

c# - 使用动态基地址 retrofit 客户端

c# - 如何从现有 Controller 自动生成 retrofit 接口(interface)?

C#虚拟串口写入超时异常

c# - MVC 在布局代码之前执行 View 代码并破坏我的脚本顺序

c# - 带有依赖注入(inject)的工厂模式,serviceProvider 总是返回 null

asp.net-mvc - 在 ASP.NET MVC Core 中显示来自字节数组的图像

c# - 在不丢失 View 状态的情况下防止在浏览器刷新时重新提交 webform

c# - ASP.NET Core InvalidOperationException : Unable to resolve service for type DbContext while attempting to activate UserStore

c# - 我可以访问未直接引用的库

c# - 带有 retrofit 的多部分内容