JSON serialization is undoubtedly one of the most important performance improvements within the ASP.
Inside the ASP, we can insert a custom mediatypeformatter (media formatter),
Plainly, it is based on HTTP content-type Application/json
To determine which media formatter to use
Implementation, remember to introduce the Jil package
Public classJilformatter:mediatypeformatter {Private ReadOnlyOptions _jiloptions; PrivateMethodInfo _method; PublicJilformatter () {//the time format to serialize_jiloptions =NewOptions (dateFormat:DateTimeFormat.ISO8601); //Media TypeSupportedmediatypes.add (NewMediatypeheadervalue ("Application/json")); //Add utf8encoding encodingSupportedencodings.add (NewUTF8Encoding (encodershouldemitutf8identifier:false, throwOnInvalidBytes:true)); //Add unicodeencoding encodingSupportedencodings.add (NewUnicodeEncoding (Bigendian:false, byteOrderMark:true, throwOnInvalidBytes:true)); } //To determine whether the type is deserialized Public Override BOOLcanreadtype (Type type) {if(Type = =NULL) { Throw NewArgumentNullException ("type"); } return true; } //determine if serialization type Public Override BOOLcanwritetype (Type type) {if(Type = =NULL) { Throw NewArgumentNullException ("type"); } return true; } //asynchronously deserializes an object of a specified type. Public Overridetask<Object>readfromstreamasync (Type type, Stream readstream, System.Net.Http.HttpContent content, Iformatterlogger Formatterlogger) {returnTask.fromresult (Deserializefromstream (Type, readstream)); } Private Objectdeserializefromstream (Type type, Stream readstream) {Try { using(varReader =NewStreamReader (Readstream)) { returnJSON. Deserialize (reader, type, _jiloptions); } } Catch { return NULL; } } //asynchronously serializes an object of a specified type. Public OverrideTask writetostreamasync (Type type,Objectvalue, Stream writestream, System.Net.Http.HttpContent content, TransportContext transportcontext) { varStreamWriter =NewStreamWriter (Writestream); Json. Serialize (value, StreamWriter, _jiloptions); StreamWriter.Flush (); returnTask.fromresult (Writestream); } }
Adding to the media formatter collection
Public Static voidRegister (httpconfiguration config) {//Web API Configuration and Services//Web API RoutingCONFIG. Maphttpattributeroutes (); Config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional});
globalconfiguration.configuration.formatters[0] =NewJilformatter (); } }
Control class
Public classHomecontroller:apicontroller { Public StaticList<market> =NewList<market>(); PublicHomeController () {}StaticHomeController () {Markets.add (NewMarket {Id =1, Name ="1", Datetimenow =DateTime.Now}); Markets.add (NewMarket {Id =2, Name ="1", Datetimenow =DateTime.Now}); Markets.add (NewMarket {Id =3, Name ="1", Datetimenow =DateTime.Now}); Markets.add (NewMarket {Id =4, Name ="1", Datetimenow =DateTime.Now}); Markets.add (NewMarket {Id =5, Name ="1", Datetimenow =DateTime.Now}); } PublicIenumerable<market>Get () {return; } Publichttpresponsemessage Post ([frombody]market value) {Markets.add (value); return Newhttpresponsemessage (Httpstatuscode.ok); } PublicHttpresponsemessage Put (intID, [frombody]market value] { varMarket = markets.where (x = X.id = =ID). FirstOrDefault (); if(Market = =NULL) return Newhttpresponsemessage (Httpstatuscode.notfound); Market. Name=value. Name; return Newhttpresponsemessage (Httpstatuscode.ok); } PublicHttpresponsemessage Delete (intID) {varresult = Markets.remove (markets.where (x = = X.id = =ID). FirstOrDefault ()); returnresult?Newhttpresponsemessage (httpstatuscode.accepted):Newhttpresponsemessage (Httpstatuscode.notfound); } }
We visit through postman
We're in post an entity
Let's get a little bit.
You can already see the entity that was just post.
Source: http://pan.baidu.com/s/1i3o6KD3
ASP. NET MVC Vnext series http://www.cnblogs.com/liek/p/4634294.html
It's my biggest motivation to get your affirmation!
Using JIL serialization JSON to promote ASP. NET Web API performance