Web API control registration and rewriting Defaulthttpcontrollerselector, Apicontrolleractionselector, Apicontrolleractioninvoker

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.