Skip to content

Cheap Windows Hosting ASP.NET

ASP.NET Hosting Cheap | Faster

X
  • Hosting Award
  • Hosting Comparison
  • Hosting Review
  • Tutorial
  • Contact
Menu

ASP.NET Tutorial :: Remove XML Formatter from ASP.NET Web API Applications

Posted on June 9, 2016June 16, 2016 by Mary Bacine

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.ASP.NET Tutorial :: Remove XML Formatter from ASP.NET Web API Applications

On the off chance that I review a Web API ask for in many programs I get a XML reaction like this:

ASP.NET Tutorial :: Remove XML Formatter from ASP.NET Web API Applications

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):

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

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.

ASP.NET Tutorial :: Remove XML Formatter from ASP.NET Web API Applications

An accept header like this:

Accept: text/html,application/xhtml+xml,*/*;q=0.9

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)
RouteTable.Routes.MapHttpRoute(
name: “StockApi”,
routeTemplate: “stocks/{action}/{symbol}”,
defaults: new
{
symbol = RouteParameter.Optional,
controller = “StockApi”
}
);

// WebApi Configuration to hook up formatters and message handlers
RegisterApis(GlobalConfiguration.Configuration);
}

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)
.ToList() ;
foreach (var match in matches)
config.Formatters.Remove(match);
}
}

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:

ASP.NET Tutorial :: Remove XML Formatter from ASP.NET Web API Applications

HostForLIFE.eu
HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools. Plesk completes requests in seconds. It is included free with each hosting account. Renowned for its comprehensive functionality – beyond other hosting control panels – and ease of use, Plesk Control Panel is available only to HostForLIFE’s customers. They offer a highly redundant, carrier-class architecture, designed around the needs of shared hosting customers.
Posted in ASP.NET, Hosting TutorialTagged ASP.NET Tutorial, best and affordable Classic ASP Hosting, best and cheap Classic ASP Hosting, best and comparison Classic ASP Hosting, cheap and reliable Classic ASP Hosting.

Post navigation

Affordable SQL Server 2016 Hosting Recommendation – UKWindowsHostASP.NET
Best Windows & ASP.NET Hosting Provider in UK

Related Post

  • Best Cheap ASP.NET MVC 6 Hosting in Europe
  • eNom VS GoDaddy – Cheap ASP.NET Hosting Comparison
  • Benefits of Social Marketing
  • Cheap and Best Windows ASP.NET Hosting :: HostForLIFE.eu VS VidaHost
  • Best and Cheap ASP.NET MVC 5 Hosting Service
  • ASP.NET MVC 6 Tutorial :: Handle Multiple Submit Buttons On The Same Form in MVC 6
  • Best and Cheap IIS 8.5 Hosting Provider
  • Best and Cheap Windows Server 2012 Hosting – HostForLIFE.eu
  • Affordable ASP.NET Core 1.0 RC2 Hosting – Cheap Windows Hosting ASP.NET

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Best SQL 2016 Hosting

Categories

  • ASP.NET
  • ASP.NET Ajax
  • ASP.NET MVC
  • Entity Framework
  • Hosting Comparison
  • Hosting Review
  • Hosting Tutorial
  • IIS
  • Joomla
  • nopCommerce
  • Silverlight
  • SQL Server

Recent Posts

  • A2 Hosting vs ASPHostPortal ASP.NET Hosting – Who is Your Favourite?
  • Looking for A2 ASP.NET Hosting Alternatives?
  • Windows Dedicated Server with HostForLIFE.eu. Is it Good?
  • Let’s Move From Ionos ASP.NET Hosting
  • Choosing Your Best ASP.NET Core Hosting Provider: ASPHostPortal.com VS GoDaddy in Comparison

Our Partner

  • Best ASP.NET Hosting
  • Best Windows Hosting
  • Cloud ASP.NET Hosting
  • ECommerce Hosting Review
  • Recommended ASP.NET
  • Reliable Windows Hosting
  • Review ASP.NET Hosting
  • Windows Hosting Bulletin
  • Windows Hosting Leaders
  • Windows ASP.NET Hosting
  • Windows Hosting Review
  • Best Cheap Hosting ASP.NET
  • Hosting Review ASP.NET
  • Full Trust Hosting ASP.NET
  • Cheap Australia ASP.NET Hosting
  • Review Core ASP Hosting
  • Cheap Australia ASP.NET Hosting
  • Best Cloud ASP.NET Hosting
  • Creative Videos
  • Business Vendor
  • Reliable ASP.NET Hosting
  • Cheap ASP.NET Hosting Review
  • European ASP.NET Hosting
  • UK ASP.NET Hosting
  • ASP.NET Hosting Review
  • India ASP.NET Hosting
  • Best India ASP.NET Hosting
  • Cheap ASP.NET Hosting Review
  • Best ASP.NET Hosting Review
  • Easy Hosting ASP.NET
  • Cheap Windows Hosting
  • Reliable ASP.NET Hosting Review
Created by cheapwindowshostingasp.net
  • Hosting Award
  • Hosting Comparison
  • Hosting Review
  • Tutorial
  • Contact