When the Web API writes API interface, the default return is to serialize your object and return it as XML, so how do you get it back as JSON, here are two methods: Method one: (Change the Configuration method)
Locate the Global.asax file and add a sentence in the Application_Start () method:
. the code is as follows:GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear ();
After modification:
. the code is as follows:protected void Application_Start () {
Arearegistration.registerallareas ();
Webapiconfig.register (globalconfiguration.configuration);
Filterconfig.registerglobalfilters (globalfilters.filters);
Routeconfig.registerroutes (routetable.routes); Returning the API to JSON
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear ();
}
This returns the result is JSON type, but there is a bad place, if the result returned is a string type, such as 123, the returned JSON will become "123";
The workaround is to customize the return type (return type is Httpresponsemessage)
. the 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 Two: (Balm method)
Instead of using the automatic serialization object in the Web API, it is cumbersome to change the configuration in method one and to process JSON with a return value of string, and then return it after serializing itself.
. 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.getencoding ("UTF-8"), "a Pplication/json ")}; return result; }
Method Two is my recommended method, in order not to repeat the code in each interface, so the encapsulation of a method so that the use of more convenient.
. the code is 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 = serializer. Serialize (obj);
} httpresponsemessage result = new Httpresponsemessage {Content = new stringcontent (str, encoding.getencoding ("UTF-8"), " Application/json ")};
return result; }
Method Three: (the most troublesome method)
Method One is the simplest, but the lethality is too large, all the returned XML format will be killed, then the method three can only let the API interface to kill the XML, return JSON
First write a class that handles the return:
. the code is as follows:public class Jsoncontentnegotiator:icontentnegotiator {private readonly jsonmediatypeformatter _jsonformatter;
Public Jsoncontentnegotiator (Jsonmediatypeformatter formatter) {_jsonformatter = formatter;}
Public contentnegotiationresult Negotiate (type type, httprequestmessage request, Ienumerable<mediatypeformatter > formatters) {var result = new Contentnegotiationresult (_jsonformatter, New Mediatypeheadervalue ("Application/json ")); return result; } }
Locate the WebApiConfig.cs file in App_start, open the Find register (httpconfiguration config) method
Add the following code:
. the code is as follows:var jsonformatter = new Jsonmediatypeformatter ();
Config. Services.replace (typeof (Icontentnegotiator), New Jsoncontentnegotiator (Jsonformatter));
Add the following 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 ();
Config. Services.replace (typeof (Icontentnegotiator), New Jsoncontentnegotiator (Jsonformatter)); }
Method Three if the returned result is a string type, such as 123, the returned JSON becomes "123", and the workaround is the same as method one.
In fact, the Web API will automatically convert the returned object to XML and JSON two format coexistence form, method one and method three is to kill the return of the XML, and method two is the custom return.
Two ways that the web Api returns JSON