ASP.NET Tutorial – Remove XML Formatter from ASP.NET Web API Applications | ASP.NET Web API’s default yield arrangement should be JSON, yet when I get to my Web APIs utilizing the program location bar I’m continually seeing a XML come about. At the point when chipping away at AJAX application I like to test huge numbers of my AJAX APIs with the program while taking a shot at them. While I can’t investigate all demands along these lines, GET appeals are anything but difficult to test in the program particularly on the off chance that you have JSON seeing alternatives set up in your different programs.
On the off chance that I review a Web API ask for in many programs I get a XML reaction like this:
Web API checks the HTTP Accept headers of an appeal to figure out what kind of yield it ought to return by searching for substance wrote that it has formatters enrolled for. This programmed arrangement is one of the immense gimmicks of Web API in light of the fact that it makes it simple and straightforward to demand various types of yield from the server.
On account of programs it just so happens most send Accept headers that resemble this (Chrome for this situation):
Web API reviews the whole rundown of headers from left to right (in addition to the quality/need banner q=) and tries to discover a media sort that matches its rundown of upheld media sorts in the rundown of formatters enlisted. For this situation it matches application/xml to the Xml formatter thus that is the thing that gets returned and showed.
To check that Web API to be sure defaults to JSON yield as a matter of course you can open the appeal in Fiddler and pop it into the Request Composer, uproot the application/xml header and see that the yield returned returns in JSON.
An accept header like this:
alternately forgetting the Accept header inside and out ought to provide for you a JSON reaction. Interestingly enough Internet Explorer 9 likewise shows JSON on the grounds that it does exclude an application/xml Accept header:
Accept: text/html, application/xhtml+xml, */*
which for once really appears to be more sensible.
Removing the XML Formatter
We can’t without much of a stretch change the program Accept headers (really you can by digging into the config however its a somewhat of a bother), so would we be able to change the conduct on the server? At the point when chipping away at AJAX applications I have a tendency to not be occupied with XML results and I generally need to see JSON results at any rate amid advancement. Web API utilizes a gathering of formatters and you can experience this rundown and uproot the ones you would prefer not to utilize – as a part of this case the XmlMediaTypeFormatter.
To do this you can work with the HttpConfiguration object and the static GlobalConfiguration article used to design it:
protected void Application_Start(object sender, EventArgs e)
// Action based routing (used for RPC calls)
symbol = RouteParameter.Optional,
controller = “StockApi”
// WebApi Configuration to hook up formatters and message handlers
public static void RegisterApis(HttpConfiguration config)
// remove default Xml handler
var matches = config.Formatters
.Where(f => f.SupportedMediaTypes
.Where(m => m.MediaType.ToString() == “application/xml” ||
m.MediaType.ToString() == “text/xml”)
.Count() > 0)
foreach (var match in matches)
That LINQ code is very much a bite of settled accumulations, however it does the trap to evacuate the formatter in light of the substance sort. You can likewise search for the particular formatter (XmlMediatTypeFormatter) by its write name which is less complex, however its ideal to hunt down the backed sorts as this will work regardless of the possibility that there are other custom formatters included.
Once evacuated, now the program appeal results in a JSON reaction: