The ASP is a new generation of HTTP Web service development frameworks that can be used in a variety of cross-platform mobile devices, in addition to the rapid development of Visual Studio 2012 (built in the Web API for ASP 4). , if you want to develop a RESTful application, then using the ASP. NET Web API should be an ideal solution. The ASP. NET Web API supports both JSON and XML output formats, and responds to the format of the response based on the Accept target sent by the browser, but also because it allows some developers of Web APIs to go on the road It seems that undesired is annoying, and this article will introduce how to enable the ASP. NET Web API to respond to JSON results so that the JSON results can be seen directly from any browser.
Using the browser to test the results of the ASP. NET Web API
Let's start with a new ASP. NET MVC 4 case:
Then choose the Web API paradigm:
The following is a preset controller canvas for creating a Web API, and the Get () method in the preset also includes some of the presets:
This time you can directly activate the case:
After the first activation, you must first change the network path to /api/values to see the preset valuescontroller results, and if we use IE9 to open the site, you will not see the content, but directly prompt the download:
After the download, the file is in JSON format:
It is very troublesome to use IE to download the file and to open it with other text editors every time you want to see the results of the Web API.
If we switch to Google Chrome to connect to the exact same Web site, the result is a copy of the XML format that is not in the JSON format, as shown below:
How the ASP. NET Web API automatically uses the client's HTTP requirements and returns the corresponding results format
Since the ASP. NET Web API is the content format of the Accept headers that are sent out on the browser side, if you want to analyze their technical details, you can use the Fiddler HTTP packet analysis software to help us see exactly which HTTP the browser sends out The target to the server side.
Let's take a look at what IE9 sent to the HTTP request (Request Headers), as shown below:
You can see that the Accept includes text/html,application/xhtml+xml , and */* , and these three are not in the ASP. NET Web API's xmlformatter support vector type, so IE9 responds to the JSON preset format:
Accept:text/html, Application/xhtml+xml, */*
Let's take a look at the HTTP request headers sent by Google Chrome (Request Headers), as shown below:
You can see that the Accept includes text/html,application/xhtml+xml,application/xml and */* these four, and Application/xml This is exactly one of the supported media types of the ASP. Xmlformatter, so Google Chrome responds to the XML format:
accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Fixed the supported media type (mediatype) for the ASP. Xmlformatter Web API
Because the developers who simply want to see the JSON response, every time with Fiddler is too troublesome, if our Web API completely does not need to respond to XML format, you can select the XML response to support the response to shut down, its code is simple, as long as the following this itinerary to the Gl The Application_Start () event of the Obal.asax file (which is available on the first or last line of this file) can be set up:
globalconfiguration. Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear ();
The following diagram is set up:
After the fix, press F6 (or Ctrl + Shift + B) to re-build the program (build), and then re-view the same address in the browser, you can find the results of the response into JSON format, as shown below:
If we're in the middle of Visual Studio 2012, you can also see what all of the Xmlformatter in the ASP. NET Web API supports, as shown below you should be able to discover that Applicat Ion/xml and text/xml these two:
At the same time, we can also use the "Watch Pane" to view all the media types supported by the Jsonformatter in the ASP. Application/json and Text/json these two: