Dan*_* Ko 12 c# odata asp.net-core-webapi
我Microsoft.AspNetCore.Odata 7.3在 Asp.net Web Api Core 3.1 项目中使用。
事实证明,该库将服务器的 TimeZone 添加到 Ef 输出中的 DateTimes 中。
EF 本身返回带有 的数据DateTimeKind.Unspecified。
因此,我希望 odata lib 能够省略时区转换,因为 webApi 服务的行为方式就是这样。
问题是如何使odata不添加服务器的时区或以utc(Z格式)返回所有内容。
尝试通过 NewtonSoft serializerSettings 设置它,但它不适用于 OData 端点
services.AddOData();
services.AddMvc(options => {options.EnableEndpointRouting = false;});
services
.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
});
Run Code Online (Sandbox Code Playgroud)
感谢帮助!
更新:在 Ef 级别上将 DateTimeKind 设置为 Utc(通过ValueConverters)允许服务器将日期时间正确转换为本地日期时间。但日期时间仍然不是以 Z 格式返回,而是以服务器的本地时区返回。
小智 8
我有类似的设置(也将时间戳视为 EF 层上的 UTC)并遇到了相同的问题。
就我而言,我可以使用以下扩展方法修复它:https://learn.microsoft.com/en-us/previous-versions/aspnet/mt135699 (v=vs.118)?redirectedfrom=MSDN
示例(Startup.cs)
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
if (odataRoutePrefix != null && edmModel != null)
{
endpoints.EnableDependencyInjection();
endpoints.Filter().OrderBy().MaxTop(100).Count().Select();
endpoints.MapODataRoute("odata", odataRoutePrefix, edmModel);
endpoints.SetTimeZoneInfo(TimeZoneInfo.Utc);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2947 次 |
| 最近记录: |