自托管的WebAPI控制器日期为美国而非英国格式

时间:2016-10-26 10:51:35

标签: c# asp.net-web-api

我已经阅读了很多有关此问题的帖子,但未能将其修复。

我有一个自我托管的API,位于英国......所以想要英国的日期时间格式

基本上,我将日期作为DD / MM / YYYY作为GET发送到我的APIController,他们以MM / DD / YYYY的形式进入控制器。

所以,当我发送2016年12月31日时,我收到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");
    }

有人可以给我任何指示吗?

1 个答案:

答案 0 :(得分:3)

您不应将日期序列化程序绑定到特定的日期格式或文化。日期格式应仅用于演示目的。

JSON是一种数据格式,而不是演示文稿格式。 JSON没有指定日期格式但JavaScript没有 - 为了符合要求,使用日期的JavaScript格式通常是一个好主意。

SO: The “right” JSON date format

相关问题