ASP. NET WEBAPI Usage Summary

Source: Internet
Author: User

If you want the server to return JSON or XML directly, consider using WebService, WCF, or WEBAPI. WebService is XML-based and slow in efficiency, WCF can return JSON, but the configuration is cumbersome. The WEBAPI is simple and flexible compared to the first two, and the efficiency is good. is the first choice for making API interfaces on the ASP.

New. NET Framework Web application, template selection Webapi, the default template has done most of the configuration for you, run the program directly and browser access to the default controller Valuescontroller,/api/values can see the effect. The WEBAPI Access routing profile is located under the App_start folder, and the configuration method differs little from the MVC route.

If you want to change the XML format that is returned by default to a JSON format, you can add it in the Application_Start method of the global file:

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

The returned data is formatted as JSON instead of XML.

However, the serialization method used is the system's own, if you want to customize, you can directly return to the Httpresponsemessage class in the controller, but Httpresponsemessage need to build their own.

[Acceptverbs ("Get","Post")]//Configure the accepted request type.  Publichttpresponsemessage Demo () {stringJsonstr = Jsonconvert.serializeobject (New{Id =Ten, Name ="ka"}); Httpresponsemessage result=Newhttpresponsemessage {Content =NewStringcontent (Jsonstr,encoding.getencoding ("UTF-8"),"Application/json")}; returnresult; }

You can see the results by accessing the demo method.

Reference post: https://www.cnblogs.com/elvinle/p/6252065.html

ASP. NET WEBAPI Usage Summary

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.