Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control serializer in JsonResult aka Json function in ASP.Net MVC?

Tags:

Is there a way to configure what JSON serializer is used when returning JSON via the JsonResult in the controller:

public ActionResult SomeJsonFunction()
{
  var x = SomeModelCode.SomeModelFunction();
  return Json(x);
}

It looks like the default is the JavaScriptSerializer. I would love to be able to use the DataContractJsonSerializer, but cannot find any documentation on how to do this.

like image 858
Jason Jackson Avatar asked Jan 01 '09 01:01

Jason Jackson


1 Answers

Check the source to see how JsonResult is implemented. Derive from ActionResult with your DataContractJsonSerializer implementation. Right now it won't be easy to use the Json() helper method, but you could create your own helper method in a layer supertype controller. Your new helper method would return your new ActionResult derivation.

like image 119
Matt Hinze Avatar answered Sep 30 '22 19:09

Matt Hinze