8

I'm ramping up on MVC 4's Web API and I'm a bit confused about the default formatting. I want the API data to be in JSON. However, it's returning it in XML. According to the MVC 4 getting started video at http://www.asp.net/web-api/videos/getting-started/your-first-web-api, it should be JSON by default. But when I create a new Web Api project and run the sample, I get this:

<ArrayOfstring><string>value1</string><string>value2</string></ArrayOfstring>

I've been running around in circles trying to get this in JSON but apparently there is a lot of misinformation about this. Such as:

  1. If I add "application/json" to the content type header, it should return JSON. This doesn't work, but I'm suspecting I don't have the header variable name right as I'm not finding the exact name to use. I've tried "Content-Type" and "contentType" in the request headers with no luck. Besides, I want JSON by default, not according to header info.
  2. If I create a JsonFormatter and add it in Application_Start GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonNetFormatter(serializerSettings)); It should do the trick. But I gathered this depreciated as none of the examples are working.

What could I do, something simple preferably, to output data in JSON format by default?

Levitikon
  • 7,749
  • 9
  • 56
  • 74
  • 2
    Don't be fooled by your browser. Chrome and FF will render JSON data as XML. Double check the output coming from your application in Fiddler. – Nick Oct 15 '12 at 22:56
  • 1
    The correct spelling of the header you are looking for is "Accept". Content-type is to describe what IS being sent, not what you would like to be sent. And the JSON formatter is already in the formatter collection by default. – Darrel Miller Oct 16 '12 at 11:26
  • Wow, it never dawned on me it could be a browser issue. I was using FireFox, doh! – Levitikon Oct 16 '12 at 13:09

6 Answers6

19

Add this to GLOBAL.ASAX to get the response to be application/json

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

So it should look like this in context:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    BundleTable.Bundles.RegisterTemplateBundles();
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}

OR if you need to preserve XML as a media type you could instead edit App_Start/WebApiConfig.cs:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );

Which makes JSON the default response for a web browser but returns text/html.

Source

Community
  • 1
  • 1
Justin
  • 26,443
  • 16
  • 111
  • 128
4

I want the API data to be in JSON. However, it's returning it in XML

How are you accessing your webapi? Are you using Chrome to access your webapi service (as Nick has mentioned in the comment)? Chrome adds the Accept header application/xml to the request...

If I add "application/json" to the content type header, it should return JSON

Try setting the 'Accept' header instead.

If I create a JsonFormatter and add it in Application_Start GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonNetFormatter(serializerSettings)); It should do the trick. But I gathered this depreciated as none of the examples are working.

If the accept header of the request is application/xml, content negotiation will still pick the XmlMediaTypeFormatter and will return application/xml. One more thing, the formatter for JSON is called JsonMediaTypeFormatter, and it is already in position 0 of the Formatters collection.

Maggie Ying
  • 10,095
  • 2
  • 33
  • 36
0

If you only want JSON then clear the formatters collection of all its defaults and then add just the JSON one back in.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
0

You don't need to remove xml support to get JSON response. For GET requests, you should set the Accept-header - not the Content-Type header. For other HTTP verbs, it depends. Fully explained here.

http://blogs.msdn.com/b/kiranchalla/archive/2012/02/25/content-negotiation-in-asp-net-mvc4-web-api-beta-part-1.aspx

Bonus: Use Google Chrome's Postman plugin to test REST APIs. Highly recommended :)

https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

John Korsnes
  • 2,277
  • 2
  • 20
  • 31
0

You can also write below in GLOBAL.ASAX

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

this also works for me.

KomalJariwala
  • 1,977
  • 3
  • 23
  • 32
0

Quoting Felipe Leusin (How do I get ASP.NET Web API to return JSON instead of XML using Chrome?)

I just add the following in App_Start/WebApiConfig.cs class in my MVC Web API project.

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );

That makes sure you get json on most queries, but you can get xml when you send text/xml.

If you need to have the response Content-Type as application/json please check Todd's answer below: https://stackoverflow.com/a/20556625/287145

Community
  • 1
  • 1
Raphael Costa
  • 86
  • 1
  • 3