3

I just created a Web API project in Visual Studio 2013, and when I go to a sample restful URL in my browser, like http://localhost/values/5, it returns XML. How do I make it so that it returns JSON by default instead of XML? Something in my Global.asax file? Thanks!

1 Answer 1

4

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

Accept: application/json

or you can remove xml media type support at all 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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

For me, I had to put this code not in Global.asax, but instead in App_Start/WebApiConfig.cs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.