How to Make ASP. net web api respond to JSON format by default

Source: Internet
Author: User

By default, WEB APIs use the request sent by the browser Header of the client to determine the returned format, as shown in red. The browser uses XML by default. Therefore, if we do not limit the Accept value of the Header, XML is returned by default. The data formats returned by the wen api server mainly include XML and JSON. Our idea is to directly remove the XML format from the server, so naturally we have to return the JSON format.

 

 

Solution:

In Application_Start of Global. asax. cs

Join

GlobalConfiguration. Configuration. Formatters. XmlFormatter. SupportedMediaTypes. Clear ();

 

Save and regenerate F5.

 

 

We can see that the Accept value in the red area remains unchanged, but the returned data has become a lovely JSON. OVER

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.