在ASP.Net MVC JsonResult又名Json的功能控制串行?串行、又名、功能、Net

2023-09-04 00:26:58 作者:這個夏天々守護你

有没有一种方法来配置什么JSON序列用于通过JsonResult控制器返回JSON时:

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);
}

它看起来像默认为 JavaScriptSerializer 。我很想能够使用的DataContractJsonSerializer,但无法找到如何做到这一点的任何文档。

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.

推荐答案

检查源看JsonResult如何实现。从推导的ActionResult你DataContractJsonSerializer实现。现在,它不会很容易使用JSON()辅助方法,但你可以创建一个层超控制了自己的助手方法。您的新助手方法将返回新的ActionResult推导。

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.