Custom JSON serialization in ASP.NET MVC
When using JSON ActionResult in ASP.NET MVC, the default behavior is to serialize the enumeration as a numeric value rather than its string name. While JSON.Net should solve this problem, it may not be the default serializer in your application.
Check Web.config settings
Contrary to popular belief, setting the default JSON serializer in web.config does not work with ASP.NET MVC. In fact, ASP.NET MVC 4 still uses JavaScriptSerializer to handle JSON operations.
Implementing JSON.Net in MVC4
To integrate JSON.Net in MVC4, you need to create a custom JsonNetResult class. Please refer to the following article for guidance:
Apply JSON.Net to controller parameters
To apply JSON.Net to controller action parameters during model binding, create your own ValueProviderFactory implementation. Please make sure to register your implementation using the following code:
<code class="language-csharp">ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories .OfType<JsonValueProviderFactory>().Single()); ValueProviderFactories.Factories.Add(new MyJsonValueProviderFactory());</code>
Consider using the built-in JsonValueProviderFactory or refer to the following article for more information:
The above is the detailed content of How to Customize JSON Serialization in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!