我在Breeze的JsonMediaTypeFormatter
的設置中遇到問題。 我會做的是,由WebAPI 發送和接收的json日期始終以UTC工作。Breeze的WebApi控制器如何使用UTC DateTime?
據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。
EDIT 2
首先,我已經更新了微風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());
}
}
}
其次,我創建了一個CustomBreezeConfig.cs類(周杰倫下面描述的代碼),一,我叫BreezeConfig文件夾中,但這個新的嘗試沒有奏效。
問候,
貝爾納多·帕切科
請讓我們知道這是否解決了它。如果是這樣,請檢查答案下的複選標記。這樣做會幫助我們解決這個問題。謝謝。 – Ward
@Jay我保留了相同的代碼(上面描述的BreezeWebApiConfig.cs類和CustomBreezeConfig.cs類)並更新至v 0.80.5。但是,服務器發送給客戶端的json日期仍然不是UTC(最後沒有Z)。問候。 –
Bernardo,它不會是,但它會在客戶端上正確轉換(到UTC日期)這就是DataType.parseDateFromServer方法的作用。所以看看當時調用返回的結果中的日期。 –