gpt4 book ai didi

javascript - Breeze 的 WebApi Controller 如何与 UTC 日期时间一起工作?

转载 作者:行者123 更新时间:2023-11-30 10:36:01 24 4
gpt4 key购买 nike

我在 Breeze 的 JsonMediaTypeFormatter 设置中遇到问题。我要做的是 WebAPI 发送和接收的 json 的日期始终以 UTC 工作。

根据 this document ,可以通过为 JsonSerializerSettings

将属性 DateTimeZoneHandling 设置为 DateTimeZoneHandling.Utc

但是那没有用。

调查 this source code ,我意识到可能影响这种行为的是为 this other issue 所做的黑客攻击。 .

通过删除下面的所有代码,一切正常。

    //jsonSerializerSettings.Converters.Add(new IsoDateTimeConverter
//{
// DateTimeFormat = "yyyy-MM-dd\\THH:mm:ss.fffK"
//});

如何在不删除 Hack 的情况下处理这种情况?

编辑 1

我第一次尝试设置如下:

var jsonFormatter = Breeze.WebApi.JsonFormatter.Create();
jsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
jsonFormatter.SupportedEncodings.Add(new UTF8Encoding(false, true));
GlobalConfiguration.Configuration.Formatters.Insert(
0, jsonFormatter);

但这没有用,返回的日期不是 UTC。

编辑 2

首先,我已将 Breeze 库更新到 0.80.3 版本。

在我的 App_Start 文件夹中,我有这个 BreezeWebApiConfig.cs 文件:

[assembly: WebActivator.PreApplicationStartMethod(
typeof(Partner.App_Start.BreezeWebApiConfig), "RegisterBreezePreStart")]
namespace Partner.App_Start
{
public static class BreezeWebApiConfig
{
public static void RegisterBreezePreStart()
{
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "BreezeApi",
routeTemplate: "api/{controller}/{action}"
);

var jsonFormatter = Breeze.WebApi.JsonFormatter.Create();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
jsonFormatter.SupportedEncodings.Add(new UTF8Encoding(false, true));

GlobalConfiguration.Configuration.Formatters.Insert(
0, jsonFormatter);

// Apply query parameters, expressed as OData URI query strings,
// to results of Web API controller methods that return IQueryable<T>
GlobalConfiguration.Configuration.Filters.Add(
new Breeze.WebApi.ODataActionFilter());
}
}
}

其次,我在名为 BreezeConfig 的文件夹中创建了一个 CustomBreezeConfig.cs 类(使用 Jay 在下面描述的代码),但是这个新尝试没有奏效。

问候,

贝尔纳多·帕切科

最佳答案

从 breeze v 0.80.3 开始,我们添加了自定义 json 序列化程序设置的功能,breeze 用于查询和保存。它涉及添加一个服务器端类,该类是新 Breeze.WebApi.BreezeConfig 类的子类。这个子类看起来像:

 public class CustomBreezeConfig : Breeze.WebApi.BreezeConfig {

/// <summary>
/// Overriden to create a specialized JsonSerializer implementation that uses UTC date time zone handling.
/// </summary>
protected override JsonSerializerSettings CreateJsonSerializerSettings() {
var baseSettings = base.CreateJsonSerializerSettings();
baseSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
return baseSettings;
}
}

服务器端项目中出现的 Breeze.WebApi.BreezeConfig 子类的任何实例现在都将被自动发现并用于自定义 breeze 的配置。

请让我们知道这是否有帮助(或没有帮助)。

关于javascript - Breeze 的 WebApi Controller 如何与 UTC 日期时间一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com