Using the JSON.NET Date Format Returned Using a Controller Action

In my MVC4 project, I have a controller action as follows:

public ActionResult GetJson() { var serialized = JsonConvert.SerializeObject(DateTime.Now); return Json(DateTime.Now, JsonRequestBehavior.AllowGet); } 

The answer to the browser is in the old ASP.NET format:

"/ Date (1358987787691) /"

However, I know that MVC4 uses json.net by default, and that json.net uses the ISO8601 format for dates.

In the above code, the serialized variable contains (what I need):

"\" 2013-01-24T13: 39: 12.7182079 + 13: 00 \ ""

Why is returning Json (DateTime.Now) not (apparently) using json.net?

I also tried putting the following line in my global.asx file:

 var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = false; 

but to no avail.

+4
source share
1 answer

You can make some changes to the factory controller and create your own controller, which inherits from the standard controller, but uses a custom json formatter.

+1
source

All Articles