web API Write API interface when the default return is to serialize your object in the form of XML return, then how to return to the JSON, here are some good ways to introduce
When the Web API writes the API interface by default, it returns the serialization of your object and returns it as XML, so how do you get it back to JSON? The following is a description of two methods: method one: (change collocation method) Locate the Global.asax file and add a sentence: code to the Application_Start () method as follows: GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear (); modified: The code is as follows: protected void Application_Start () { arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); filterconfig.registerglobalfilters ( globalfilters.filters); routeconfig.registerroutes (routetable.routes); //Make API return to json GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear (); } The result returned is the JSON type, but there is a bad place, if the result returned is string type, such as 123, the returned JSON becomes "123"; The solution is to customize the return type ( return type is Httpresponsemessage) code is as follows: Public httpresponsemessage postusername (user user) { String UserName = user.username; httpresponsemessage result = new Httpresponsemessage {Content = new stringcontent (uSername,encoding.getencoding ("UTF-8"), "Application/json")}; return result; } Method II: (Snake Balm method) Method One to change the configuration, but also to handle the return value of string type of JSON, very troublesome, rather than the Web API in the automatic serialization of objects, and then back to the serialization of the code is as follows: public Httpresponsemessage postuser (user user) { javascriptserializer serializer = new JavaScriptSerializer (); String str = serializer. Serialize (user); httpresponsemessage result = new Httpresponsemessage {Content = new stringcontent (str, ENCODING.G Etencoding ("UTF-8"), "Application/json")}; return result; } Method Two is the method I recommend, in order not to write the code over and over again in every interface. , so it's much more convenient to encapsulate it as a way to use it. Code as follows: public static httpresponsemessage Tojson (Object obj) { string str; if (obj is string || obj is Char) { str = obj. ToString (); } else { javascriptserializer serializer = new JavaScriptSerializer (); str = SE Rializer. Serialize (obj); } httpresponsemessage result = new Httpresponsemessage {Content = new stringcontent (str, encoding.getencoding ("UTF-8"), "Application/json")}; return result; Bsp Method III: (the most troublesome method) method One of the simplest, but the lethality is too large, all the returned XML format will be killed, then method three can only let the API interface to kill XML, return json First write a process to return the class: code as follows: public class jsoncontentnegotiator:icontentnegotiator { private readonly Jsonmediatypeformatter _jsonformatter; Public jsoncontentnegotiator (Jsonmediatypeformatter formatter) { _jsonformatter = formatter; } Public contentnegotiationresult Negotiate (type type, Http Requestmessage request, ienumerable<mediatypeformatter> formatters) { var result = new Contentnegotiationresult (_jsonformatter, New Mediatypeheadervalue ("Application/json")); return result; } } Find the WebApiConfig.cs file in App_start, open the Find register (httpconfiguration config) method Add the following code: Code as follows: var jsonformatter = new Jsonmediatypeformatter (); config. Services.replace (typeof (Icontentnegotiator), New Jsoncontentnegotiator (Jsonformatter)); Add code as follows: The code is as follows: public static void Register (httpconfiguration config) { config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{action}/{id}", defaults:new {id = routeparameter.optional} ); var jsonformatter = new Jsonmediatypeformatter (); conf Ig. Services.replace (typeof (Icontentnegotiator), New Jsoncontentnegotiator (Jsonformatter)); } Method three If the result returned is a string type, such as 123, the returned JSON becomes "123", and the solution is the same as method one. In fact, the Web API automatically turns the returned objects into XML and JSON two formats, and method Three is to kill the return of the XML, and method two is to customize the return.