ASP. NET MVC Tour--the application and source code analysis of the sixth station Actionfilter

Source: Internet
Author: User

  

This article we began to look at Actionfilter, from the name of the fact that the Actionfilter is the action on the filter, right, then the action on the filter about a few???

This question is actually quite simple, because we heard that MVC itself is a very extensible framework, naturally, layers have interception, layers have filtering, right, such as we see the following controller class.

     Public Abstract class controller:controllerbase, Iactionfilter, Iauthenticationfilter, Iauthorizationfilter, IDisposable, Iexceptionfilter, Iresultfilter, Iasynccontroller, IController, Iasyncmanagercontainer    {    }

From the controller of this parent class, we can see that there are 5 filter, for example: Iactionfilter,iauthenticationfilter,iauthorizationfilter,iexceptionfilter , Iresultfilter,

Well, first of all, we'll start with the first actionfilter.

One: Iactionfilter analysis

Now that we know that Iactionfilter is an interface, the next thing that interests us is what's in this actionfilter, like the one below me.

    //    //Summary://Defines the methods that is used in an action filter.     Public InterfaceIactionfilter {//        //Summary://called after the action method executes. //        //Parameters://Filtercontext://The filter context.        voidonactionexecuted (ActionExecutedContext filtercontext); //        //Summary://called before an action method executes. //        //Parameters://Filtercontext://The filter context.        voidonactionexecuting (ActionExecutingContext filtercontext); }

From the above code, we can see in fact this interface only two methods, one is onactionexecuting, one is onactionexecuted, look at this name you should understand

In fact, before and after the action is executed, right, that way, smart you think of the application scenario, log logs, access to the action, to facilitate subsequent charges ~ ~ ~ Next I

Let's see how we can implement these two methods.

1. Implement Actionfilter in an override manner

Now we all know that the Controller class has implemented this interface, then our own xxxcontroller has just inherited the parent controller, so in the face of this situation, we can use

Override to achieve this, as I realized below.

1      Public classHomecontroller:controller2     {3          Publicactionresult Index ()4         {5             returnView ();6         }7 8         protected Override voidonactionexecuting (actionexecutingcontext filtercontext)9         {Ten             filterContext.HttpContext.Response.Write ("Hello"); One  A             Base. OnActionExecuting (filtercontext); -         } -  the         protected Override voidonactionexecuted (actionexecutedcontext filtercontext) -         { -           FilterContext.HttpContext.Response.Write ("World"); -  +             Base. OnActionExecuted (filtercontext); -         } +}

So we can easily add a pleasant realization, is not very simple, but carefully think, such a method is still a little limited, that is, we override the dependency is too strong, for example, only

Class extends Iactionfilter, and then we'll see if there's a more flexible way to do it.

2. Custom class extends Iactionfilter

To be highly flexible, we have to make this implementation class an "atomic unit," and with this atomic unit, we can easily apply this non-uncoupling atom to all places.

Go, right, this atom can be implemented in C # with attribute, like this:

1      Public classMyactionfilterattribute:attribute, Iactionfilter2     {3          Public voidonactionexecuted (actionexecutedcontext filtercontext)4         {5FilterContext.HttpContext.Response.Write ("Hello");6         }7 8          Public voidonactionexecuting (actionexecutingcontext filtercontext)9         {TenFilterContext.HttpContext.Response.Write (" World"); One         } A}

OK, now that we've got an atomic myactionfilterattribute feature, we can then apply this myactionfilterattribute anywhere, like this:

1      Public class Homecontroller:controller 2     {3        [Myactionfilter]4public           ActionResult Index ()5        {6             return  View (); 7         }8     }

3. ActionFilterAttribute

In addition to implementing the following attribute features and Iactionfilter interfaces, we can also inherit the ActionFilterAttribute features provided to us by an MVC framework , and can't wait to see it ~

1     //2     //Summary:3     //represents the base class for filter attributes.4[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, inherited =true, AllowMultiple =false)]5      Public Abstract classActionfilterattribute:filterattribute, Iactionfilter, Iresultfilter6     {7         //8         //Summary:9         //Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class.Ten         protectedActionFilterAttribute (); One  A         // -         //Summary: -         //called by the ASP. NET MVC Framework after the action method executes. the         // -         //Parameters: -         //Filtercontext: -         //The filter context. +          Public Virtual voidonactionexecuted (ActionExecutedContext filtercontext); -         // +         //Summary: A         //called by the ASP. NET MVC Framework before the action method executes. at         // -         //Parameters: -         //Filtercontext: -         //The filter context. -          Public Virtual voidonactionexecuting (ActionExecutingContext filtercontext); -         // in         //Summary: -         //called by the ASP. NET MVC Framework after the action result executes. to         // +         //Parameters: -         //Filtercontext: the         //The filter context. *          Public Virtual voidonresultexecuted (ResultExecutedContext filtercontext); $         //Panax Notoginseng         //Summary: -         //called by the ASP. NET MVC framework before the action result executes. the         // +         //Parameters: A         //Filtercontext: the         //The filter context. +          Public Virtual voidonresultexecuting (ResultExecutingContext filtercontext); -}

From this attribute can be seen, it integrates iactionfilter, iresultfilter, naturally have the two interface methods, well, not much to say, we come to realize this abstract class bar.

namespacewebapplication2.controllers{ Public classHomecontroller:controller { [myactionfilter]  PublicActionResult Index () {returnView (); }    }     Public classMyactionfilterattribute:actionfilterattribute { Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {Base.        OnActionExecuting (Filtercontext); }         Public Override voidonactionexecuted (ActionExecutedContext filtercontext) {Base.        OnActionExecuted (Filtercontext); }    }}

Two: Source code analysis

The best way to parse the source code is to want you to download a reflector plugin so that we can get to the runtime's debug code and see the call stack, especially the call heap

Stack "is very important to you.

1. First our next breakpoint in the OnActionExecuting method, such as:

2. Fallback to the previous stack through the call stack,

This method is actually very interesting, from the method name can be seen, in fact, it is a recursive mode, that is, "onactionexecuting" and "" into the stack execution Begininvokeactionmethod "

The "Rewind execution onactionexecuted" method, because there is a very good-looking statement, such as:

this. Invokeactionmethodfilterasynchronouslyrecursive (num);

Well, more details await you to study, hope this article is helpful to you ~ ~ ~

ASP. NET MVC Tour--the application and source code analysis of the sixth station Actionfilter

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.