Each platform should have an exception handling policy, where the exception handling policy is only for the current thread exception generated by the user request, not including the uncaught exception resulting from asynchronous processing, and for exception handling suggestions you can see the Exception handling of Microsoft Enterprise Library application Block
Abscommonexceptionfilterattribute, as with information validation, exceptions are handled by Attrbute.
Using System.Net; Using System.Net.Http; Using System.Web.Http.Filters; <summary>//WEBAPI Exception response handling///</summary> public abstract class Abscommonexceptionfilterattribute : exceptionfilterattribute {//<summary>//Raises the exception Event///</summary& Gt <param name= "ActionExecutedContext" >the context for the action.</param> public override void Onexce Ption (Httpactionexecutedcontext actionexecutedcontext) {string exceptionstr = string. Empty; HttpStatusCode StatusCode = Httpstatuscode.internalservererror; if (this. Allowexception) {//Allow exception information to be included in the returned result exceptionstr = this. HandleException (ActionExecutedContext, ref statusCode); }//Here you can fix the return value Actionexecutedcontext.response = ActionExecutedContext.Request.CreateResponse (Statusco DE, New Stringcontent (EXCEPTIONSTR)); ActionexecutEdcontext.response = new Httpresponsemessage (statusCode) {Content = new stringcontent (EXCEPTIONSTR)}; }///<summary>//whether to allow return of exception information, test environment should be allowed to return exceptions to the secondary test, formal environment should prohibit return exception///</summary> Prot ected abstract bool Allowexception {get;} <summary>////exception handling strategies///</summary>//<param name= "ActionExecutedContext" >< ;/param>//<param name= "ex" ></param>///<returns></returns> protected V Irtual string HandleException (Httpactionexecutedcontext actionexecutedcontext, ref HttpStatusCode StatusCode) { The exception can be classified here, the corrected return HttpStatusCode//can also be encapsulated or converted to the exception, it is recommended to use the Microsoft Enterprise Library exception Handling application Block if (Actionexecutedcontext.exception is notimplementedexception)//{//StatusCode = Htt pstatuscode.notimplemented; } return actionExecutedContext.Exception.ToString (); } } Sub-class example
public class Commonexceptionfilterattribute:abscommonexceptionfilterattribute { protected override bool Allowexception { get {return true;} } }
Register attribute and add the following code to the Register method of the default generated Webapiconfig file
Config. Filters.add (New Commonexceptionfilterattribute ());
WebAPI user authentication Tamper-proof Implementation (iii) exception information processing Abscommonexceptionfilterattribute