Directly on the code
1 /// <summary>2 ///message agent processing, which is used to capture these special exception information3 /// </summary>4 Public classCustomerrormessagedelegatinghandler:delegatinghandler5 {6 protected OverrideTaskSendAsync (httprequestmessage request, CancellationToken CancellationToken)7 {8 return Base. SendAsync (Request, CancellationToken). Continuewith9 {TenHttpresponsemessage response =Responsetocompletetask.result; OneHttperror error =NULL; A if(Response. Trygetcontentvalue outerror)) - { - //Custom error Handling the //error. Message = "Error occurred with this interface call"; - } - if(Error! =NULL) -{//This is a return class that I have created + varResultmsg =Newresultmsg {StatusCode = (int) Statuscodeenum.httpurleror, Info =error. Messagedetail}; - return Newhttpresponsemessage {Content =Newstringcontent (Resultmsg.tojson (), +System.Text.Encoding.GetEncoding ("UTF-8"),"Application/json"), StatusCode =Httpstatuscode.ok}; A } at Else - { - returnresponse; - } - }); - } in}
Then just register the CS file and locate the Global.asax file
1 protected voidApplication_Start ()2 {3 Arearegistration.registerallareas ();4 //filterconfig.registerglobalfilters (globalfilters.filters);5 //routeconfig.registerroutes (routetable.routes);6 //bundleconfig.registerbundles (bundletable.bundles);7 globalconfiguration.configure (webapiconfig.register);8GLOBALCONFIGURATION.CONFIGURATION.FILTERS.ADD (NewErrorHandler ());9}
Finally done, the effect:
1 {2 " StatusCode " 404 , 3 " Info " " The action matching the request could not be found on the controller "Studytask". ",4 "Data"null5 }
Uniform return format in WEBAPI frame