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. |
|||||||||||||||||||||
|
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:
|
|||||||||||||||||||||
|
MVC4 Quick Tip #3–Removing the XML Formatter from ASP.Net Web API In
like so:
|
|||||||||||||||||||||
|
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 |
||||
In the Global.asax I am using the code below. My url to get JSON is http://www.digantakumar.com/api/values?json=true
|
|||||||||
|
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.
|
|||||||||||||||||||||
|
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! |
|||||||||
|
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 used a global action filter to remove
Seems to work. |
|||
|
I found the Chrome app "Advanced REST Client" excellent to work with REST services. You can set the Content-Type to |
||||
|
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. |
|||
|
Some time has passed since this question was asked (and answered) but another option is to override the Accept header on the server during request processing using a MessageHandler as below:
Where You'll need to register it of course. You can either do this globally:
or on a route by route basis:
And since this is a message handler it will run on both the request and response ends of the pipeline much like an
|
||||
|
Here is a solution similar to jayson.centeno's and other answers, but using the built-in extension from
The solution was primarily geared toward supporting $format for OData in the early releases of WebApi, but it also applies to the non-OData implementation, and returns the
It allows you to tack |
|||
|
You just change the
|
|||||||||||||||||||||
|
Its unclear to me why there is all of this complexity in the answer. Sure there are lots of ways you can do this, with query strings, and headers and options... but what I believe to be the best practice is simple. You request a plain URL (ex: http://yourstartup.com/api/cars) and in return you get json. You get json, with the proper response header:
In looking for an answer to this very same question, I found this thread, and had to keep going because this accepted answer doesn't work exactly. I did find an answer which I feel is just too simple not to be the best one: Set the default WebAPI formatter I'll add my tip here as well.
I do have a question of where the defaults (at least the ones I am seeing) come from. Are they .NET defaults, or perhaps created somewhere else (by someone else on my project). Anways, hope this helps. |
|||
|
Here is the easiest way that I have used in my applications. Add given below 3 lines of code in
Asp.net web API will automatically serialize your returning object to JSON and as the |
|||
|
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. |
|||||
|
protected by Win Sep 29 '15 at 21:33
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?