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.