Craig Shearer
Craig Shearer

Reputation: 14521

Using JSON.NET date format returned from 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 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.

Upvotes: 4

Views: 1146

Answers (1)

Ph0en1x
Ph0en1x

Reputation: 10067

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

Upvotes: 1

Related Questions