How To Get ASP.NET Web API to Return JSON
first remove xml format :
config.Formatters.Remove(config.Formatters.XmlFormatter);
accepts text/html requests and returns application/json responses :
public class BrowserJsonFormatter : JsonMediaTypeFormatter
{
public BrowserJsonFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-www-form-urlencoded"));
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
this.SerializerSettings.Formatting = Formatting.Indented;
}
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
base.SetDefaultContentHeaders(type, headers, mediaType);
headers.ContentType = new MediaTypeHeaderValue("application/json");
}
}
Register like so:
config.Formatters.Add(new BrowserJsonFormatter());
References
http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome
https://github.com/aspnet/Mvc/issues/1765