Disabling the xml formatter in ASP.NET Web API–The easy way

OK, I have not posted yet on the fact that WCF Web API is now reborn as ASP.NET Web API. I just did! Yes it is true. That in itself is very awesome news which I am sure many of you have already heard about. If not, go jump to Scott Gu’s blog here right now! I still have a post planned on that. Yes, I HAVE to post….later!

Moving on so that this idea doesn’t get lost in the warehouse of “to be posted” posts.

A common request people are asking is how to disable / remove the xml formatter so that their web apis ONLY return JSON regardless of the accept header.

There’s a few different common approaches to doing this. Including:

  • Create a custom message handler which overrides accept headers. I did one a long time ago here. The handler is then registered in the config.
  • Remove the formatter from the collection. There’s a pretty easy way to do this as the formatters collection actually exposes an XmlFormatter property thus you can just remove it from the collection. Below is a snippet showing how to do this using the global config when hosted in ASP.NET.
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);

 

This evening I was reading my friend Shawn Wildermuth’s blog  post “WebAPI for the MVC guy” (which you should also check out) and I saw he was using a  significantly more complex variant of the second option by looping through the formatters to remove the XmlFormatter. I am sure he would not have done that had he known about the formatter hanging off of the formatters collection, however it’s probably not the most obvious.

Looking at the code I then realized there’s a third. It’s by far the least amount of code of any of the approaches I’ve seen and it’s so dead simple

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

 

What this does is remove the supported media types from the XmlFormatter. Our content negotiation algorithm looks at that collection to determine if that formatter is a match. By making it empty, well it becomes no-op.

Any questions?

This entry was posted in ASP.NET Web API. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Nagareddy S Reddy

    Easiest way is with one line

    config.Formatters.Remove(config.Formatters.XmlFormatter);

  • Gene

    I like to default to JSON including in the browser, but I don’t want to disable XML entirely. As most browsers include “application/xml” in their accept headers, even though the JsonFormatter is the first formatter by default, it will fall through to the XmlFormatter and you will see Xml in the browser.

    You can get around this by adding “text/html” to the JsonFormatter (this assumes it is the first item in the index as it should be by default):

    GlobalConfiguration.Configuration.Formatters[0].SupportedMediaTypes.Add(new MediaTypeHeaderValue(“text/html”));

    Now when a browser sends accept headers of “text/html” and “application/xml” it will trigger the JsonFormatter rather than falling back to the XmlFormatter. But if you really want Xml you can still get it by specifically requesting  ”application/xml”.

  • AAD

    Thank you, you guys did a great job with the web api, i’m loving it !

    I would like to see some guidance on the ways to do caching.  There is not to much info  on asp.net/web-api, henrik’s blog, google etc