MVC4 filter use and how to control all action and part action

Source: Internet
Author: User

The filters in MVC are divided into four types: Iactionfilter (action Filter), Iauthorizationfilter (authorization filter), Iexceptionfilter (Exception filter), Iresultfilter (Result filter) literal translation, do understand it.

Here is the iactionfilter example, there are two methods in this interface, namely: onactionexecuting (Action execution before execution) and onactionexecuted (Action execution after execution),

Now we want to have all the actions in a controller execute this filter and we need to rewrite the method inside.

   Public classLogincontroller:controller {//        //GET:/login/         PublicActionResult Index () {returnView (); }           PublicActionResult Login () {stringName = httpcontext.request["UserName"]; viewdata["name"] =name; returnView (); }        protected Override voidonactionexecuting (ActionExecutingContext filtercontext) {//object[] attrs = FilterContext.ActionDescriptor.GetCustomAttributes (typeof (NoFilter), true); //if (attrs. Length = = 1)//has NoFilter property//{            //return; //}            stringName = filtercontext.httpcontext.request["UserName"]; if(string. IsNullOrEmpty (name)) {FilterContext.HttpContext.Response.Write ("<script>alert (' name cannot be empty! ');</script>");            FilterContext.HttpContext.Response.End (); }        }    }
View Code

This will execute the filter before each action executes.

Here's how to make the action of index not execute, just execute on login. There are 2 ways to achieve this:

The first: The code is modified as follows:

  Public classLogincontroller:controller {//        //GET:/login/[NoFilter] PublicActionResult Index () {returnView (); }                 PublicActionResult Login () {stringName = httpcontext.request["UserName"]; viewdata["name"] =name; returnView (); }        protected Override voidonactionexecuting (ActionExecutingContext filtercontext) {//filter out the action labeled NoFilter            Object[] Attrs = FilterContext.ActionDescriptor.GetCustomAttributes (typeof(NoFilter),true); if(Attrs. Length = =1)//has NoFilter property            {                return; }            stringName = filtercontext.httpcontext.request["UserName"]; if(string. IsNullOrEmpty (name)) {FilterContext.HttpContext.Response.Write ("<script>alert (' name cannot be empty! ');</script>");            FilterContext.HttpContext.Response.End (); }        }    }     Public classNofilter:filterattribute, Iactionfilter { Public  voidonactionexecuting (ActionExecutingContext filtercontext) {//        }         Public  voidonactionexecuted (ActionExecutedContext filtercontext) {//        }    }
View Code

The second type: The code is modified as follows:

  Public classLogincontroller:controller {//        //GET:/login/         PublicActionResult Index () {returnView (); } [Loginfilter] PublicActionResult Login () {stringName = httpcontext.request["UserName"]; viewdata["name"] =name; returnView (); }        //protected override void OnActionExecuting (ActionExecutingContext filtercontext)//{        //    //filter out the action labeled NoFilter//object[] attrs = FilterContext.ActionDescriptor.GetCustomAttributes (typeof (NoFilter), true); //if (attrs. Length = = 1)//has NoFilter property//    {        //return; //    }        //String name = filtercontext.httpcontext.request["UserName"]; //if (string. IsNullOrEmpty (name))//    {        //FilterContext.HttpContext.Response.Write ("<script>alert (' name cannot be empty!        ');</script> "); //filterContext.HttpContext.Response.End (); //    }        //}    }     Public classLoginfilter:filterattribute, Iactionfilter { Public voidonactionexecuting (ActionExecutingContext filtercontext) {stringName = filtercontext.httpcontext.request["UserName"]; if(string. IsNullOrEmpty (name)) {FilterContext.HttpContext.Response.Write ("<script>alert (' name cannot be empty! ');</script>");            FilterContext.HttpContext.Response.End (); }        }         Public voidonactionexecuted (ActionExecutedContext filtercontext) {//        }    }
View Code

Add: 1. Custom filter filters must inherit FilterAttribute.

2. Define a filter to define the order in which the filters are executed. For example: Mark on action:

[NoFilter (order=2)]

[Loginfilter (Order=1)]

This executes the loginfilter before executing the nofilter when the action is executed.

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.