Breeze的Web Api控制器如何与UTC DateTime配合使用?

时间:2012-12-21 18:55:59

标签: javascript breeze

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

根据this document,可以通过为DateTimeZoneHandling

将属性DateTimeZoneHandling.Utc设置为JsonSerializerSettings来实现

然而这不起作用。

调查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 lib更新为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下面描述的代码),但是这个新尝试没有用。

此致

Bernardo Pacheco

4 个答案:

答案 0 :(得分:2)

从breeze v 0.80.3开始,我们添加了自定义jreeze序列化器设置的功能,这些设置是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的配置。

如果这有帮助(或没有),请告知我们。

答案 1 :(得分:1)

当你说添加DateTimeZoneHandling不起作用时,你是如何尝试设置它的?

您可以尝试在源代码中的“Converters.Add”调用(从上方)上方添加此行(不删除“hack”),并告诉我它是否有效。

 jsonSerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

我同意它仍然很笨拙,因为这意味着你必须修改微风源。因此,如果它确实有效,我们将尝试提供一些方法来允许您从格式化程序外部设置它。请告诉我们。

答案 2 :(得分:1)

请尝试使用breeze v 0.80.5以及相应的发行说明。希望'时间应该现在应该正常往返。

答案 3 :(得分:0)

我解决了这个黑客的问题,它仍然闻起来。

app.vm.run.js中的

app.vm.run = (function ($, ko, dataservice, router) {
var currentRunId = ko.observable(),
    // run will be an entity
    run = ko.observable(),
...
   save = function () {
        this.run().lastUpdate(makeDatetimeUTC(moment().toDate()));
        this.run().runStart(makeDatetimeUTC(this.run().runStart()));
        this.run().runEnd(makeDatetimeUTC(this.run().runEnd()));
        dataservice.saveChanges();
        // the test r === run() succeeds because the local run is a 
        //  ko.observable which is bound to the run in the cache
        var r = dataservice.getRunById(currentRunId());
    },
...

})($, ko, app.dataservice, app.router);
myScripts.js中的

// Here is a real pain in the neck.
//  For some reason, when the entity is saved, it shows up on the server as UTC datetime
//  instead of local.  Moment parses everything as local by default, so the formatDate function
//  used to get a display value needs to be converted to utc before it is returned to the server.
// 
// This function takes the value of the dependentObservable in the entity
//  and converts it to a string which can be stored back into the entity before sending
//  it to the server.
//
// The reason I need to do that is so that it displays properly after the save.      
//  The date seems to be handled properly by the server.

var makeDatetimeUTC = function(localDatetime) {
    var datestring = formatDate(localDatetime);
    var utc = moment.utc(datestring);
    return formatDate(utc);
};

var formatDate = function(dateToFormat) {
    if (dateToFormat === null ||dateToFormat === undefined || dateToFormat.length === 0)
        return "";

    // intermediate variable is not needed, but is good for debugging
    var formattedDate = moment(dateToFormat).format('MM/DD/YYYY hh:mm A');
    return formattedDate;
    },
    formatObservableDate = function(observable) {
        if (ko.isObservable(observable))
            return observable(formatDate(observable()));
        else
            throw new Error("formatObservableDate expected a ko.Observable ");
    };
相关问题