Asp.Net MVC5代码的筛选器(ActionFilter)执行递归链表分析
来源:互联网 发布:bt条码打印软件 编辑:程序博客网 时间:2024/05/31 00:39
我们用筛选器控制Action运行前后需要处理的事情
public abstract class ActionFilterTestcs:FilterAttribute,IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{ }
public void OnActionExecuting(ActionExecutingContext filterContext)
{ }
}
抽象一个ActionFilterTestcs
在Controller中比如HomeController中继承此抽象类
public class FooAttribute:ActionFilterTestcs
{
int a = 1;
}
在Action Index上面用上这两个筛选类如下:
[Bar]
[Foo]
public ActionResult Index()
{ return View();}
Filter 执行过程从 BeginInvokeActionMethodWithFilters开始
protected internal virtual IAsyncResult BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters, AsyncCallback callback, object state)
{
Func<ActionExecutedContext> endContinuation = null;
BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState)
{
ActionExecutingContext preContext = new ActionExecutingContext(controllerContext, actionDescriptor, parameters);
AsyncResult innerAsyncResult = null;
Func<Func<ActionExecutedContext>> beginContinuation = () => {
innerAsyncResult = BeginInvokeActionMethod(controllerContext, actionDescriptor, parameters, asyncCallback, asyncState);
return () =>
new ActionExecutedContext(controllerContext, actionDescriptor, false /* canceled */, null /* exception */)
{
Result = EndInvokeActionMethod(innerAsyncResult)
};
}; // need to reverse the filter list because the continuations are built up backward
Func<Func<ActionExecutedContext>> thunk = filters.Reverse().Aggregate(beginContinuation,
(next, filter) => () => InvokeActionMethodFilterAsynchronously(filter, preContext, next));
endContinuation = thunk();
if (innerAsyncResult != null)
{ // we're just waiting for the inner result to complete
return innerAsyncResult;
}
else
{ // something was short-circuited and the action was not called, so this was a synchronous operation
SimpleAsyncResult newAsyncResult = new SimpleAsyncResult(asyncState);
newAsyncResult.MarkCompleted(true /* completedSynchronously */, asyncCallback);
return newAsyncResult;
}
};
EndInvokeDelegate<ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult)
{
return endContinuation();
};
return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionMethodWithFiltersTag);
}
通过 Aggregate构建了一个Action筛选器(IList<IActionFilter>)递归链表endContinuation = thunk();是这个链表的开始执行地方。
具体过程 Filter1,beginContinuation 返回结果为Aggregate累加。MVC5的ActionFilter链表递归结构跟MVC3,4 有一定的区别,前者BeginInvokeActionMethodWithFilters返回的是Func<Func<ActionExecutedContext>>,后者直接Func<ActionExecutedContext>。
而执行过程,OnActionExecuted则是延缓了执行,从Func<Func<ActionExecutedContext>>到 Func<ActionExecutedContext>他通过了递归经过了一层剥离方法然后再执行OnActionExecuted执行过程为 :
Filter1.OnActionExecuting-》Filter2.OnActionExecuting-》-》Filter3.OnActionExecuting-》BeginInvokeActionMethod-》EndInvokeActionMethod-》Filter1.OnActionExecuted-》Filter2.OnActionExecuted-》Filter2.OnActionExecuted
MVC3,4执行过程为:Filter1.OnActionExecuting-》Filter2.OnActionExecuting-》-》Filter3.OnActionExecuting-》InvokeActionMethodFilter-》Filter3.OnActionExecuted-》Filter2.OnActionExecuted-》Filter1.OnActionExecuted
- Asp.Net MVC5代码的筛选器(ActionFilter)执行递归链表分析
- asp.net mvc 1中ActionFilter的执行层级问题
- ActionFilter递归链剖析
- ActionFilter的执行机制
- asp.net mvc 之旅 —— 第六站 ActionFilter的应用及源码分析
- ActionFilter for MVC asp.net
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(81)-数据筛选(万能查询)
- MVC四大筛选器—ActionFilter&ResultedFilter
- ASP.NET MVC5修改namespace的问题
- asp.net MVC5 sitemap 的使用
- [ASP.NET]分析MVC5源码,并实现一个ASP.MVC
- asp.net mvc5 安装
- ASP.net MVC5 简介
- asp.net+mvc5新建控制器时出现的,运行所选代码生成器时错误
- 求教各位高手:ASP.net MVC使用ActionFilter判断访问页面的权限怎么实现?
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET MVC是如何运行的(二)
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET MVC是如何运行的(三)
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET MVC是如何运行的(三)
- 微信开放“附近的小程序”功能,要连接线上线下
- Google秘密开发新操作系统Fuchsia,UI设计曝光
- 物联网创业公司升哲科技获千万美元融资
- 雷军现身淘宝问答:小米6青春版是假的,亮银目前无法量产
- 俄罗斯解除对微信封锁
- Asp.Net MVC5代码的筛选器(ActionFilter)执行递归链表分析
- 微软花了几个小时阐述他们打算如何超越亚马逊,亚马逊表示受宠若惊 | Build 2017
- 苹果和高通争的不只是专利授权费,更是下一个时代?
- 1026. Table Tennis (30)
- nodejs http post请求java后台数据,java端拿不到参数的问题
- JVM Exception in thread "main" java.lang.StackOverflowError
- java修行之路(四)
- Google关闭Android 7.x Beta项目,迎接全新Android O
- 注意力数字乔迁新居 “旋转空间”设计别有内涵