How to return default json for visual studio web api

I just created a web API project in Visual Studio 2013, and when I go to the sample calm URL in my browser, for example http://localhost/values/5, it returns XML. How to make it return default JSON instead of XML? Is there something in my file Global.asax? Thank!

+3
source share
1 answer

By default, the Web API returns a service document in AtomPub format. To request JSON, you can add the following header to the HTTP request:

Accept: application/json

or you can completely remove xml multimedia type support in Global.asax

public class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}
+3
source

All Articles