Using the newer ASP.NET Web API, in Chrome I am seeing XML - how can I change it to request JSON so I can view it in the browser? I do believe it is just part of the request headers, am I correct in that?
|
I just add the following in App_Start/WebApiConfig.cs class in my MVC Web API project.
That makes sure you get json on most queries, but you can get xml when you send text/xml. If you need to have the response Content-Type as application/json please check Todd's answer below. |
|||||||||||||||||||||
|
If you do this in the
If you are not using the MVC project type and therefore did not have this class to begin with, see this answer for details on how to incorporate it. |
|||||||||||||||||||||
|
MVC4 Quick Tip #3–Removing the XML Formatter from ASP.Net Web API In
like so:
|
|||||||||||||||||||||
|
I like Felipe Leusin's approach best - make sure browsers get JSON without compromising content negotiation from clients that actually want XML. The only missing piece for me was that the response headers still contained content-type: text/html. Why was that a problem? Because I use the JSON Formatter Chrome extension, which inspects content-type, and I don't get the pretty formatting I'm used to. I fixed that with a simple custom formatter that accepts text/html requests and returns application/json responses:
Register like so:
|
|||||||||||||||||||||
|
In the Global.asax I am using the code below. My url to get JSON is http://www.digantakumar.com/api/values?json=true
|
|||||||||
|
In the WebApiConfig.cs, add to the end of the Register function:
source: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization |
||||
Have a look at content negotiation in the WebAPI. These (Part 1 & Part 2) wonderfully detailed and thorough blog posts explain how it works. In short, you are right, and just need to set the |
|||||||||
|
As the question is Chrome-specific, you can get the Postman extension which allows you to set the request content type. |
|||
|
One quick option is to use the MediaTypeMapping specialization. Here is an example of using QueryStringMapping in the Application_Start event:
Now whenever the url contains the querystring ?a=b in this case, Json response will be shown in the browser. |
|||||
|
I choose these codes since my default is json but if i want to use the XML format. I'll just append the
Thanks everyone! |
|||||
|
Using RequestHeaderMapping works even better, because it also sets the Content-Type = application/json in the response header, which allows Firefox (with JSONView add-on) to format the response as JSON.
|
|||||
|
Don't use your browser to test your API. Instead, try to use an HTTP client that allows you to specify your request, such as CURL, or even Fiddler. The problem with this issue is in the client, not in the API. The web API behaves correctly, according to the browser's request. |
|||||||||
|
I find the Chrome app "Advanced REST Client" excellent for working with REST services. You can set the Content-Type to application/json among other things: https://chrome.google.com/webstore/detail/hgmloofddffdnphfgcellkdfbfbjeloo |
|||
|
I used a global action filter to remove
Seems to work. |
|||
|
From MSDN Building a Single Page Application with ASP.NET and AngularJS (about 41 mins in).
It should be current, I tried it and it worked. |
|||
|
Appending $format=json to the end of an MVC4+ webapi function does now return the result as json, and $format=xml returns XML. This is fine in Chrome, as it displays JSON data on-screen, but in IE you will be prompted to download the resulting json data. |
|||||
|
|
|||
|