Json.Net IsoDateTimeConverter is not working

无人久伴 提交于 2019-12-13 12:09:29

问题


I have a web application made in asp.net mvc 4

in Global.asax, i added added IsoDateTimeConverter

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
        new IsoDateTimeConverter { DateTimeFormat = "dd-MM-yyyy hh:mm" });
}

I have an ActionResult

public ActionResult GetDate()
{
    DateTime dateTime = DateTime.Now;
    return Json(dateTime, JsonRequestBehavior.AllowGet);
}

However, this actionResult returns this:

"\/Date(1365060823129)\/"

What i am missing?


回答1:


Well I believe you got a little bit confused by all the "not precise" naming which lot of people have been using lately. The JsonFormatter (and formatters in general) is used by ASP.NET Web API.

You are not using ASP.NET Web API, you are using ASP.NET MVC 4 (those are completely separated technologies).

In ASP.NET MVC 4 the serialization logic is still done inside the JsonResult with JavaScriptSerializer, formatters are not applied here.

If you want to use Json.NET (and its IsoDateTimeConverter) with ASP.NET MVC 4 you still need to create your own ActionResult as described here.



来源:https://stackoverflow.com/questions/15805108/json-net-isodatetimeconverter-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!