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