How do I get ASP.NET Web API to return JSON instead of XML using Chrome?
Posted By: Anonymous
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?
Solution
I just add the following in App_Start / WebApiConfig.cs
class in my MVC Web API project.
config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("text/html") );
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.
NameSpace
is using System.Net.Http.Headers
.
Answered By: Anonymous
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.