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?
|
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 Global.asax: add the line:
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
|
|||||||||
|
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 |
|||||
|
I just use
That makes sure you get json on most queries, but you can get xml when you send text/xml. |
|||||||
|
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. |
|||||
|
source: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization |
|||
|
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 |
|||
|
Don't user 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 of this issue is on the client... not on the api. The web api behaves accordingly the browser's request. |
|||||
|
|
|||||
|