namespaceeworkpal.webapi{ Public classHttpnotfounddefaulthttpcontrollerselector:defaulthttpcontrollerselector { Publichttpnotfounddefaulthttpcontrollerselector (httpconfiguration configuration):Base(configuration) {} Public Overridehttpcontrollerdescriptor Selectcontroller (httprequestmessage request) {Httpcontrollerdescriptor de Criptor=NULL; Try{decriptor=Base. Selectcontroller (Request); } Catch(Httpresponseexception ex) {varCode =Ex. Response.statuscode; varresult =Neweworkresultinfo<Object> {Code =10006, Entity = ex. response.content.readasasync<Object>(). Result}; if(Code = = Httpstatuscode.notfound | | code = =httpstatuscode.methodnotallowed) {ex. Response.content=NewObjectcontent (typeof(eworkresultinfo), result, GlobalConfiguration.Configuration.Formatters.JsonFormatter); } Throw; } returnDecriptor; } } Public classHttpnotfoundcontrolleractionselector:apicontrolleractionselector { Public Overridehttpactiondescriptor selectaction (Httpcontrollercontext controllercontext) {Httpactiondescriptor Decriptor=NULL; Try{decriptor=Base. SelectAction (ControllerContext); } Catch(Httpresponseexception ex) {varCode =Ex. Response.statuscode; varresult =Neweworkresultinfo<Object> {Code =10006, Entity = ex. response.content.readasasync<Object>(). Result}; if(Code = = Httpstatuscode.notfound | | code = =httpstatuscode.methodnotallowed) {ex. Response.content=NewObjectcontent (typeof(eworkresultinfo), result, GlobalConfiguration.Configuration.Formatters.JsonFormatter); } Throw; } returnDecriptor; } } Public classCustomapicontrolleractioninvoker:apicontrolleractioninvoker { Public OverrideTaskInvokeactionasync (Httpactioncontext actioncontext, CancellationToken cancellationtoken) {varResponsemessage =Base. Invokeactionasync (Actioncontext, CancellationToken); if(Responsemessage.exception! =NULL) { varBaseexception = responsemessage.exception.innerexceptions[0]; varresult =Neweworkresultinfo<Object>{Message= Baseexception.message,//"Server Internal Error",Code =10001, Entity=responsemessage.exception}; if(baseexception istimeoutexception) {result. Code=10004; //result. Message = "Task timed out"; } returnTask.run (() =NewHttpresponsemessage () {Content=NewObjectcontent (typeof(eworkresultinfo<Object>), result, GlobalConfiguration.Configuration.Formatters.JsonFormatter)}, CancellationToken); } returnResponsemessage; } } }
Webapi.config
Public Static classWebapiconfig {/// <summary> /// /// </summary> /// <param name= "config" ></param> Public Static voidRegister (httpconfiguration config) {//Web API Configuration and ServicesConfig. Filters.add (Neweworkauthorize ()); Config. Filters.add (NewModelvalidationfilterattribute ()); Config. Services.replace (typeof(Ihttpactioninvoker),NewCustomapicontrolleractioninvoker ()); Config. Services.replace (typeof(Ihttpcontrollerselector),Newhttpnotfounddefaulthttpcontrollerselector (config)); Config. Services.replace (typeof(Ihttpactionselector),Newhttpnotfoundcontrolleractionselector ()); //Web API RoutesCONFIG. Maphttpattributeroutes (); Config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{action}"//,//{ID}//defaults:new {id = routeparameter.optional} ); varXmlformatter = config. Formatters.firstordefault (m = m isxmlmediatypeformatter); Config. Formatters.remove (Xmlformatter); Config. Formatters.JsonFormatter.SerializerSettings.ContractResolver=NewCamelcasepropertynamescontractresolver (); //CONFIG. Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Nullvaluehandling.ignore;Config. Formatters.JsonFormatter.SerializerSettings.DateFormatString ="YYYY-MM-DD hh:mm"; } }
Global Registration
Globalconfiguration.configure (Webapiconfig.register);
Web API control registration and rewriting Defaulthttpcontrollerselector, Apicontrolleractionselector, Apicontrolleractioninvoker