Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm having a hard time finding the answer to this:

I'm working on an existing application that has been partially converted over to MVC. Whenever a controller responds with a JSON ActionResult, the enums are sent as numbers opposed to the string name. It sounds like the default serializer should be JSON.Net, which should be sending the enums over as their names opposed to the integer representation, but that's not the case here.

Am I missing a web.config setting that sets this as the default serializer? Or is there another setting that needs to be changed?

share|improve this question
up vote 56 down vote accepted

In ASP.Net MVC4 the default JavaScript serializer which is used in the JsonResult class is still the JavaScriptSerializer (you can check it in the code)

I think you have confused it with the ASP.Net Web.API where JSON.Net is the default JS serializer but MVC4 doesn't use it.

So you need to configure JSON.Net to work with MVC4 (basically you need to create your own JsonNetResult), there are plenty of articles about it:

If you also want to use JSON.Net for controller action parameters so during the model binding then you need write your own ValueProviderFactory implementation.

And you need to register your implementation with:

ValueProviderFactories.Factories
    .Remove(ValueProviderFactories.Factories
                                  .OfType<JsonValueProviderFactory>().Single());
ValueProviderFactories.Factories.Add(new MyJsonValueProviderFactory());

You can use the built in JsonValueProviderFactory as an example or this article: ASP.NET MVC 3 – Improved JsonValueProviderFactory using Json.Net

share|improve this answer
    
That helps with sending JSON responses, but not with deserializing incoming JSON as the framework attempts to map incoming calls into parameters passed into your action methods . . . how do you change the serializer used by MVC for that purpose? – blaster Oct 6 '13 at 0:14
4  
If you want to use Json.NET for incoming paramters you need write your own ValueProviderFactory implementation. You can use the built in JsonValueProviderFactory as an example. And you need to register your implementation with: ValueProviderFactories.Factories.Remove( ValueProviderFactories.Factories.OfType<JsonValueProviderFac‌​tory>().Single()); ValueProviderFactories.Factories.Add(new MyJsonValueProviderFactory());. See also: dalsoft.co.uk/blog/index.php/2012/01/10/… – nemesv Oct 6 '13 at 6:49
    
+1 for how to bind controller action parameters, most question/answers i find are about just sending JSON. – snajahi Apr 12 '14 at 5:43
2  
@Marc according to the source code: aspnetwebstack.codeplex.com/SourceControl/latest#src/… the JsonResult in MVC5 still uses the old JavaScriptSerializer and does not uses JSON.net – nemesv Sep 9 '15 at 18:59
1  
@jpgrassi OfType is an extension method definied in the System.Linq namespace. Do you have the using System.Linq; in your cs file? – nemesv Sep 24 '15 at 17:04

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.