ASP. NET MVC Filter

Source: Internet
Author: User

Like a filter in MVC

1.AuthorizeAttribute

2.ActionFilterAttribute

3.HandleErrorAttribute

First, introduce Authorizeattribute, this is the authorization filter

1. Create a class that inherits Authorizeattribute

2. Override the Onauthorization method to verify permissions, such as session and Cookie, in the method

3. Add a tag to a method or class, and if you add an attribute on a method, the Onauthorization method is called before the method that adds the attribute is executed.

If you add an attribute on a class, the Onauthorization method is called before all methods in the class are executed.

If you want to call the Onauthorization method on all methods of the Controller class, add the registration under the Registerglobalfilters method under the Filterconfig class under App_start.

1 usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.Linq; 4 usingsystem.web; 5 usingSYSTEM.WEB.MVC; 6   7 namespaceFilter Filters8 {  9      Public classMyauthorizeattribute:authorizeattributeTen     {   One          Public Override voidonauthorization (AuthorizationContext filtercontext) A         {   -             //Here you can verify permissions, such as verifying session and Cookie -             //can get Session with filtercontext.httpcontext.session["Key") the             Base.   Onauthorization (Filtercontext);  -         }   -     }   -}

Second, ActionFilterAttribute use similar to Authorizeattribute, before and after executing the action and the view before and after the filter

1 usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.Linq; 4 usingsystem.web; 5 usingSYSTEM.WEB.MVC; 6   7 namespaceFilter Filters8 {  9      Public classMyactionfilterattribute:actionfilterattributeTen     {   One         //called before the action is loaded A          Public Override voidonactionexecuting (actionexecutingcontext filtercontext) -         {   -             //validation of permissions can also be done here the             Base.  OnActionExecuting (Filtercontext);  -         }   -         //called after the action is loaded -          Public Override voidonactionexecuted (actionexecutedcontext filtercontext) +         {   -             Base.  OnActionExecuted (Filtercontext);  +         }   A         //Call before loading view at          Public Override voidonresultexecuting (resultexecutingcontext filtercontext) -         {   -             Base.  Onresultexecuting (Filtercontext);  -         }   -         //called after the view is loaded -          Public Override voidonresultexecuted (resultexecutedcontext filtercontext) in         {   -             Base.  Onresultexecuted (Filtercontext);  to         }   +     }   -}

Third, Handleerrorattribute use with the above two similar, exception handling filter

1 usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.Linq; 4 usingsystem.web; 5 usingSYSTEM.WEB.MVC; 6   7 namespaceFilter Filters8 {  9      Public classMyhandleerrorattribute:handleerrorattributeTen     {   One          Public Override voidonexception (exceptioncontext filtercontext) A         {   -Exception ex = filtercontext.exception;//Get Exception -             //you can record the exception in the log. the             //jump to the friendly page to avoid displaying the error page to the user -FilterContext.HttpContext.Response.Redirect ("/err.html");  -             Base.  Onexception (Filtercontext);  -         }   +     }   -}

Filter execution Order: Authorization validation Filter--perform action before and after filter--load view before and after filter

ASP. NET MVC Filter

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.