Web api should return JSon by default

This question already has an answer here:

  • How do I get ASP.NET Web API to return JSON instead of XML using Chrome? 29 answers

  • Content negotiation looks at several things in your request including the Accept header (but also the contenttype header to infer returned results). If your request has XML in the accept header then it will go to XML.

    Below is the chrome default headers, note that it's asking for XML, hence Web API will return XML by default for chrome.

    {Connection: keep-alive Accept: text/html, application/xhtml+xml, application/xml; q=0.9, image/webp, /; q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US, en; q=0.8 Host: localhost:63586 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36 }

    You can choose in your application to ignore the accept headers, by removing them from the formatters:

    config.Formatters.JsonFormatter.MediaTypeMappings.Clear();
    

    Then add your query mapping (similarly of course for the XML formatter).

    链接地址: http://www.djcxy.com/p/20416.html

    上一篇: 如何返回Web API数据

    下一篇: Web API应该默认返回JSon