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?
相关问题
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Easiest way to get json and parse it using JQuery
- Newtonsoft DeserializeXNode expands internal array
相关文章
- json_encode 没有把数组转为json
- Livy Server: return a dataframe as JSON?
- Unexpected end of JSON input from an ajax call
- How do I do a nested list (array) of schema refere
- iconv() Vs. utf8_encode()
- Convert C# Object to Json Object
- LINQ .Include() properties from sub-types in TPH i
- How to make a custom list deserializer in Gson?
You can use as below:
Returning the correct format is done by the media-type formatter. As others mentioned, you can do this in the
WebApiConfig
class:For more, check:
In case your actions are returning XML (which is the case by default) and you need just a specific method to return JSON, you can then use an
ActionFilterAttribute
and apply it to that specific action.Filter attribute:
Applying to action:
Note that you can omit the word
Attribute
on the action decoration and use just[JsonOutput]
instead of[JsonOutputAttribute]
.It's unclear to me why there is all of this complexity in the answer. Sure there are lots of ways you can do this, with QueryStrings, 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.
If you do this in the
WebApiConfig
you will get JSON by default, but it will still allow you to return XML if you passtext/xml
as the requestAccept
headerIf 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.
Here is a solution similar to jayson.centeno's and other answers, but using the built-in extension from
System.Net.Http.Formatting
.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
Content-Type: application/json; charset=utf-8
header in the response.It allows you to tack
&$format=json
or&$format=xml
to the end of your uri when testing with a browser. It does not interfere with other expected behavior when using a non-browser client where you can set your own headers.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.