当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net mvc:ActionFilter的应用及源码分析

asp.net mvc:ActionFilter的应用及源码分析

2018年09月29日  | 移动技术网IT编程  | 我要评论

陈冠希中文网,机顶盒升级,爱与哀愁简谱

.net m 之旅 —— 第六站 actionfilter的应用及分析

这篇文章我们开始看一下actionfilter,从名字上其实就大概知道actionfilter就是action上的filter,对吧,那么action上的filter大概有几个呢???

这个问题其实还是蛮简单的,因为我们听说mvc本身就是一个扩展性极强的框架,自然就是层层有拦截,层层有过滤,对吧,比如我们看到的如下controller类。

    public abstract class controller : controllerbase, iactionfilter, iauthenticationfilter, iauthorizationfilter, idisposable, iexceptionfilter, iresultfilter, iasynccontroller, icontroller, iasyncmanagercontainer
    {
    }

从这个父类的controller中,我们就可以看到有5个filter,如:iactionfilter,iauthenticationfilter,iauthorizationfilter,iexceptionfilter,iresultfilter,

对吧,首先我们还是从第一个actionfilter说起。

一:iactionfilter解析

  现在我们知道iactionfilter是一个接口,接下来感兴趣的就是这个actionfilter里面到底是什么,比如我下面截图的这样。

    //
    // 摘要:
    //     defines the methods that are used in an action filter.
    public interface iactionfilter
    {
        //
        // 摘要:
        //     called after the action method executes.
        //
        // 参数:
        //   filtercontext:
        //     the filter context.
        void onactionexecuted(actionexecutedcontext filtercontext);
        //
        // 摘要:
        //     called before an action method executes.
        //
        // 参数:
        //   filtercontext:
        //     the filter context.
        void onactionexecuting(actionexecutingcontext filtercontext);
    }

从上面这段代码中,我们可以看到其实这个接口里面只有两个方法,一个是onactionexecuting,一个是onactionexecuted,看这名字你应该就明白

其实就是在action的前后分别执行,对吧,那这样的话,聪明的你就想到了应用场景,记录日志,获取action的访问量,以方便后续收费~~~ 接下来我

们来看看怎么来实现这两个方法。

1. 用override的方式实现actionfilter

现在大家都知道controller类已经实现了这个接口,那我们自己的xxxcontroller刚好又继承了这个父controller,所以面对这种情况,我们可以用

override来实现,比如下面我实现的这样。

 1     public class homecontroller : controller
 2     {
 3         public actionresult index()
 4         {
 5             return view();
 6         }
 7 
 8         protected override void onactionexecuting(actionexecutingcontext filtercontext)
 9         {
10             filtercontext.httpcontext.response.write("hello");
11 
12             base.onactionexecuting(filtercontext);
13         }
14 
15         protected override void onactionexecuted(actionexecutedcontext filtercontext)
16         {
17             filtercontext.httpcontext.response.write("world");
18 
19             base.onactionexecuted(filtercontext);
20         }
21     }

就这样我们就轻松加愉快的实现了,是不是很简单,但是仔细一想,这样的方法还是有一点限制的,也就是说我们override的依赖性太强了,比如说只有

classextendsiactionfilter才可以,接下来我们再看有没有更灵活的方法来实现。

2. 自定义class extends iactionfilter

要想做到高度的灵活性,我们必须将这个实现类做成一个“原子单位”,有了这个原子单位,我们就可以很方便的将这个不可拆解的原子性应用到各个地方

去,对吧,这个原子在c#中可以用attribute来实现,比如下面这样:

 1     public class myactionfilterattribute : attribute, iactionfilter
 2     {
 3         public void onactionexecuted(actionexecutedcontext filtercontext)
 4         {
 5             filtercontext.httpcontext.response.write("hello");
 6         }
 7 
 8         public void onactionexecuting(actionexecutingcontext filtercontext)
 9         {
10             filtercontext.httpcontext.response.write("world");
11         }
12     }

ok,现在我们已经得到了一个原子性质的myactionfilterattribute特性,接下来我们可以将这个myactionfilterattribute应用到任何地方,如下图:

1     public class homecontroller : controller
2     {
3         [myactionfilter]
4         public actionresult index()
5         {
6             return view();
7         }
8     }

3. actionfilterattribute

除了我们实现以下attribute特性和iactionfilter接口,我们还可以继承一个mvc框架提供给我们的actionfilterattribute特性,迫不及待的看一下吧~

 1     //
 2     // 摘要:
 3     //     represents the base class for filter attributes.
 4     [attributeusage(attributetargets.class | attributetargets.method, inherited = true, allowmultiple = false)]
 5     public abstract class actionfilterattribute : filterattribute, iactionfilter, iresultfilter
 6     {
 7         //
 8         // 摘要:
 9         //     initializes a new instance of the system.web.mvc.actionfilterattribute class.
10         protected actionfilterattribute();
11 
12         //
13         // 摘要:
14         //     called by the asp.net mvc framework after the action method executes.
15         //
16         // 参数:
17         //   filtercontext:
18         //     the filter context.
19         public virtual void onactionexecuted(actionexecutedcontext filtercontext);
20         //
21         // 摘要:
22         //     called by the asp.net mvc framework before the action method executes.
23         //
24         // 参数:
25         //   filtercontext:
26         //     the filter context.
27         public virtual void onactionexecuting(actionexecutingcontext filtercontext);
28         //
29         // 摘要:
30         //     called by the asp.net mvc framework after the action result executes.
31         //
32         // 参数:
33         //   filtercontext:
34         //     the filter context.
35         public virtual void onresultexecuted(resultexecutedcontext filtercontext);
36         //
37         // 摘要:
38         //     called by the asp.net mvc framework before the action result executes.
39         //
40         // 参数:
41         //   filtercontext:
42         //     the filter context.
43         public virtual void onresultexecuting(resultexecutingcontext filtercontext);
44     }

从这个attribute中可以看到,它整合了iactionfilter, iresultfilter,自然就有了这两个接口的方法,好了,不多说,我们来实现一下这个抽象类吧。

namespace webapplication2.controllers
{
    public class homecontroller : controller
    {
       [myactionfilter]
       public actionresult index()
        {
            return view();
        }
    }

    public class myactionfilterattribute : actionfilterattribute
    {
        public override void onactionexecuting(actionexecutingcontext filtercontext)
        {
            base.onactionexecuting(filtercontext);
        }

        public override void onactionexecuted(actionexecutedcontext filtercontext)
        {
            base.onactionexecuted(filtercontext);
        }
    }
}

二:源码分析

最好的源码分析方法,肯定是希望你下载一个reflector插件,这样我们就可以获取到运行时的可调试代码以及可以看到的调用堆栈,尤其是”调用堆

栈“,对你来说非常的重要。

1. 首先我们下一个断点在 onactionexecuting方法里面,如下图:

\

2. 通过调用堆栈回退到上一个堆栈,如图:

\

这个方法其实非常的有意思,从方法名称中可以看到,其实它是一个递归的模式,也就是”onactionexecuting" =>"进栈执行begininvokeactionmethod”

=>"退栈执行onactionexecuted“方法,因为有一个非常好看的statement,比如:

func continuation = this.invokeactionmethodfilterasynchronouslyrecursive(num);

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网