gpt4 book ai didi

asp.net-mvc-4 - ASP MVC 4 JsonResult 如何使用 ISO 8601 日期?

转载 作者:行者123 更新时间:2023-12-03 21:39:45 25 4
gpt4 key购买 nike

我刚刚安装了 ASP MVC 4,我的 JSON 日期仍然以旧格式返回 {"timestamp":"\/Date(1348070400000)\/"} .

我是 under the impression他们应该以 2012-02-18T00:54:06.8447642Z 的格式返回没有我做任何事情。

Note: JSON.NET is now an integral part of ASP.NET Web API so you can just us it out of the box.



我的 Controller 看起来像这样
public JsonResult Test()
{
return Json(new {timestamp = DateTime.Now()}, JsonRequestBehavior.AllowGet);
}

我怎样才能让它发挥作用?我需要在 Global.asax.cs 或 Web.config 中进行更改吗?

最佳答案

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



这仅适用于 ASP.NET Web API (ApiControllers)。但是返回 JsonResults 的标准 ASP.NET MVC Controller 操作仍然使用 JavaScriptSerializer .因此,如果您想使用 ISO 8601 日期,您可以通过 JSON.NET 通过编写 custom action result 来交换此序列化程序。 .只需将适当的转换器添加到串行器使用的设置中:
SerializerSettings.Converters.Add(new IsoDateTimeConverter());

您可能会问为什么他们没有为标准 Controller 实现 JSON.NET。我想这是出于兼容性原因,因为这将是一个重要的突破性变化。但我可能会将 JSON.NET 设为默认序列化程序,并为升级现有应用程序的用户提供后备选项。

关于asp.net-mvc-4 - ASP MVC 4 JsonResult 如何使用 ISO 8601 日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12506828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com