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 response 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 ISO8601 format for dates.

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

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

Why is return Json(DateTime.Now) not (seemingly) using json.net?

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

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

but to no avail.

share|improve this question
Seems I've misunderstood the documentation - json.net is the default json formatter for web API only. – Craig Shearer Jan 24 at 0:52
The answer seems to be here: james.newtonking.com/archive/2008/10/16/… – Craig Shearer Jan 24 at 0:53

1 Answer

You could make some changes in controller factory and create your controller, which is inherited from standard controller, but used custom json formatter.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.