2016-10-26 51 views
0

我已閱讀關於此問題的許多線索,但尚未得到解決。美國自行託管的WebAPI控制器日期不是英國格式

我有一個自託管API,總部設在英國的......所以希望英國的日期時間格式

基本上,我送日期爲DD/MM/YYYY爲得到我APIController和他們來了以MM/DD/YYYY形式輸入控制器。

所以,當我發送31/12/2016我收到400錯誤返回。當我發送12/31/2016它完美的作品。

這是我的服務,我試圖將文化設置爲en-GB,但這對我的問題沒有任何影響。

public APICoreService() 
    { 
     InitializeComponent(); 

     _config = new MyHttpsSelfHostConfiguration(ServiceAddress); 
     _config.MapHttpAttributeRoutes(); 
     _config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional }); 

     // upload size control 
     _config.MaxReceivedMessageSize = 5242880; // 5mb 
     _config.MaxBufferSize = 5242880; // 5mb 

     // add the controller validation filter 
     _config.Filters.Add(new InventryCoreAPIService.Filters.ValidateViewModelAttribute()); 

     // turn on error messages 
     GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; 

     _config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 

     // set the culture to solve the date problem 
     _config.Formatters.JsonFormatter.SerializerSettings.Culture = new CultureInfo("en-GB"); 
    } 

任何人都可以給我任何指針嗎?

回答

3

您不應該將日期序列化器綁定到特定的日期格式或文化。日期格式應僅用於演示目的。

JSON是一種數據格式,不是演示文稿格式。 JSON沒有指定日期格式,但是JavaScript使用了日期格式 - 爲了兼容,使用日期的JavaScript格式通常是個好主意。

SO: The 「right」 JSON date format

+0

謝謝。我試圖在錯誤的地方解決這個問題! –

相關問題