WebAPI user authentication Tamper-proof Implementation (iii) exception information processing Abscommonexceptionfilterattribute

Source: Internet
Author: User

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" >&lt ;/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

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.