Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

Thanks!

share|improve this question

1 Answer 1

up vote 29 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
2  
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<JsonValueProviderFactory>().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 at 5:43
    
I used a slightly modified version of the referenced code, and on an action with a single string input, like public ResultObject DoThis(string input) the line JSONReader.Read dies with an exception like "Inappropriate character, the text isn't JSON". Weirder is that as a URL param it's fine, but it throws in the POST body. Any thoughts? –  drzaus Aug 14 at 17:11
    
@drzaus to answer my question, it seems that a form-data encoded request, rather than a raw JSON (via POSTMAN), will trigger my issue, since it tries to deserialize essentially gibberish –  drzaus Aug 14 at 17:31

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.