How to make JSON as default media type formatter

Topics: Web Api
Jun 16, 2011 at 8:15 AM
Edited Jun 16, 2011 at 8:17 AM

In Preview 4, HTTPHostConfiguration is modified a great deal.

Today XML is default MediaFormatter if no configuration is set.

I want to know how can JSON be set as default media type formatter.

Here is how my code looks like (i have followed contactManager_Simple example from API samples). There is no explicit HTTPHostConfiguration


public class Global : System.Web.



protected void Application_Start(object sender, EventArgs



When I run the app, I see response rendered in XML in browser. I want to see this response rendered in JSON instead.

















Jun 16, 2011 at 8:31 AM

Hi Mahesh

Out of the box you need to set the accept header to application/json.

  • If you are using jQuery for example and calling $.getjson you will return json.
  • You can use a tool like Fiddler to let you create a request and add the accept header. For example see this post here which demonstrates using fiddler.





Jun 16, 2011 at 10:14 AM
Edited Jun 20, 2011 at 8:21 AM

Thanks Glenn,

But I meant on the server side...

I have tried following options..both doesn't seem to help..

Option 1

var config = HttpHostConfiguration.Create();



config.Configuration.OperationHandlerFactory.Formatters.Add(new XmlMediaTypeFormatter());

config.Configuration.OperationHandlerFactory.Formatters.Add(new JsonMediaTypeFormatter());



I have also tried to change the sequence i.e. add JSONMediaTypeFormatter first and then XML with same results.

When I navigate to URL http:\\localhost\customer\123456, output is always rendered in XML. I want this default rendering in JSON.


Option 2:

 var config = HttpHostConfiguration.Create();


config.Configuration.OperationHandlerFactory.Formatters.Insert(0,new JsonMediaTypeFormatter ());

config.Configuration.OperationHandlerFactory.Formatters.Insert(1,newXmlMediaTypeFormatter ());



In this option, as soon as I add XmlMediaTypeFormatter, output is rendered in XML. (Having Just JsonMediaTypeFormatter seems to be working though).

Am I missing anything?




Jun 20, 2011 at 8:22 AM


Is anybody else facing same issue?

Any answers?




Jun 20, 2011 at 8:38 AM

Mahesh why are you using Insert in option 2? I would just call Add and add the Json formatter first.

As far as why it is coming back as Xml, that is probably because your client is sending an accept header of "application/xml". How are you making the request, from a browser?


Jun 20, 2011 at 8:51 AM

Hi Glenn,

I have tried calling Add. That's option 1.

config.Configuration.OperationHandlerFactory.Formatters.Add(new JsonMediaTypeFormatter ());  

config.Configuration.OperationHandlerFactory.Formatters.Add(new XmlMediaTypeFormatter ());


 The problem is even though I add JSON as first formatter, I always get response back in XML from browser.

I am making request simply by typing URL (http://localhost:4124/customer/1234567) in address bar of IE8 on WInXP.

Even when I try it in Chrome, response in always in XML.





Jun 20, 2011 at 9:10 AM
Edited Jun 20, 2011 at 9:13 AM

That's not a problem, it's by design :-) and by that I mean the design of HTTP.

Your browser sends accept headers. I will bet if you look in fiddler, you will see that your browser is sending "application/xml" as part of the accept. Web API implements server driven conneg (content negotiation) and matches against the accept header that it receives in the request.

I'd ask the question though why do you want to see JSON in the browser, as XML is actually much more readable. As long as your client sends the right accept headers it will receive json.

To test, open up Fiddler and go into the request builder and put accept:application/json in the request portion. Then put your request uri and execute a GET. You will see json get returned when you look at the response body.

If you want to force json to get returned on the server, their are several options.

  • If you are using a jQuery and you make a $.getJson call, then it will send the accept header of "application/json".
  • If you are using javascript directly, then using XmlHttpRequest you can set the accept header yourself.
  • If you still want to default to json on the server under certain circumstances like whenever you query from a browser, then you can author a custom operation handler or message handler which looks at the request when it comes in and then if you determine it should be json, modifies either the accept header (on the request) or the content-type header on the response in order to force "applications/json". If you look in our ContactManger_Advanced sample at the UriExtensionChannel you will see that it uses this approach to match against a uri segment like "/xml" and sets the accept header to "application/xml"

Hope this helps.