ASP MVC 4 JsonResult how to use ISO 8601 dates?

前端 未结 1 709
孤城傲影
孤城傲影 2020-12-18 22:05

I have just installed ASP MVC 4 and my JSON dates are still coming back in the old format {\"timestamp\":\"\\/Date(1348070400000)\\/\"}.

I was under the

相关标签:
1条回答
  • 2020-12-18 22:22

    I was under the impression that they should be coming back in the format 2012-02-18T00:54:06.8447642Z without me doing anything.

    That's true only for ASP.NET Web API (ApiControllers). But standard ASP.NET MVC controller actions returning JsonResults still use the JavaScriptSerializer. So if you want to use ISO 8601 dates you could swap this serializer by JSON.NET by writing a custom action result. Just add the proper converter to the settings used by the serializer:

    SerializerSettings.Converters.Add(new IsoDateTimeConverter());
    

    You might be asking why they didn't implement JSON.NET for standard controllers. I guess it's for compatibility reasons as that would be an important breaking change. But I would probably have made JSON.NET the default serializer with a fallback option for those upgrading their existing applications.

    0 讨论(0)
提交回复
热议问题