MVC中Action的执行过程
接着上一篇:MVC控制器的激活过程
一、代码现行,该伪代码大致解析了Action的执行的过程
- try
- {
- Run each IAuthorizationFilter's OnAuthorization() method
- if(none of the IAuthorizationFilters cancelled execution)
- {
- Run each IActionFilter's OnActionExecuting() method
- Run the action method
- Run each IActionFilter's OnActionExecuted() method (in reverse order)
- Run each IResultFilter's OnResultExecuting() method
- Run the action result
- Run each IResultFilter's OnResultExecuted() method (in reverse order)
- }
- else
- {
- Run any action result set by the authorization filters
- }
- }
- catch(exception not handled by any action or result filter)
- {
- Run each IExceptionFilter's OnException() method
- Run any action result set by the exception filters
- }
二、返回主战场Action执行方法中
- public virtual bool InvokeAction(ControllerContext controllerContext, string actionName)
- {
- if (controllerContext == null)
- {
- throw new ArgumentNullException("controllerContext");
- }
- if (string.IsNullOrEmpty(actionName))
- {
- throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
- }
//描述了控制器的相关信息- ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(controllerContext);
//描述了相关Action的相关信息- ActionDescriptor actionDescriptor = this.FindAction(controllerContext, controllerDescriptor, actionName);
- if (actionDescriptor != null)
- {
//获取该action的所有Filter- FilterInfo filters = this.GetFilters(controllerContext, actionDescriptor);
- try
- {
- AuthorizationContext authorizationContext = this.InvokeAuthorizationFilters(controllerContext, filters.AuthorizationFilters, actionDescriptor);
- if (authorizationContext.Result != null)
- {
- this.InvokeActionResult(controllerContext, authorizationContext.Result);
- }
- else
- {
- if (controllerContext.Controller.ValidateRequest)
- {
//地球人应该知道这个东西干嘛的,你知道吗?- ControllerActionInvoker.ValidateRequest(controllerContext);
- }
- IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, actionDescriptor);
- ActionExecutedContext actionExecutedContext = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, actionDescriptor, parameterValues);
- this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, actionExecutedContext.Result);
- }
- }
- catch (ThreadAbortException)
- {
- throw;
- }
- catch (Exception exception)
- {
- ExceptionContext exceptionContext = this.InvokeExceptionFilters(controllerContext, filters.ExceptionFilters, exception);
- if (!exceptionContext.ExceptionHandled)
- {
- throw;
- }
- this.InvokeActionResult(controllerContext, exceptionContext.Result);
- }
- return true;
- }
- return false;
- }
- //上面的授权过程
AuthorizationContext authorizationContext = this.InvokeAuthorizationFilters(controllerContext, filters.AuthorizationFilters, actionDescriptor);- if (authorizationContext.Result != null)
- {
- this.InvokeActionResult(controllerContext, authorizationContext.Result);
- }
- protected virtual AuthorizationContext InvokeAuthorizationFilters(ControllerContext controllerContext, IList<IAuthorizationFilter> filters, ActionDescriptor actionDescriptor)
- {
- AuthorizationContext authorizationContext = new AuthorizationContext(controllerContext, actionDescriptor);
- foreach (IAuthorizationFilter current in filters)
- {
- current.OnAuthorization(authorizationContext);//很显然我们在自定义IAuthorizationFilter时,会去做事情在这里定义的。如果
//此过程,authorizationContext的Result 被你给赋值了,那么所有的其他授权认证将会终止,系统将全力执行 this.InvokeActionResult(controllerContext, authorizationContext.Result);- if (authorizationContext.Result != null)
- {
- break;
- }
- }
- return authorizationContext;
- }
三、Action连同过滤器的执行,上面谈了授权过滤器的执行
- IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, actionDescriptor);
- ActionExecutedContext actionExecutedContext = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, actionDescriptor, parameterValues);
- this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, actionExecutedContext.Result);
- protected virtual IDictionary<string, object> GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
- {
- Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
- ParameterDescriptor[] parameters = actionDescriptor.GetParameters();
- ParameterDescriptor[] array = parameters;
- for (int i = ; i < array.Length; i++)
- {
- ParameterDescriptor parameterDescriptor = array[i];
- dictionary[parameterDescriptor.ParameterName] = this.GetParameterValue(controllerContext, parameterDescriptor);
- }
- return dictionary;
- }
- protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
- {
- Type parameterType = parameterDescriptor.ParameterType;
- IModelBinder modelBinder = this.GetModelBinder(parameterDescriptor);
- IValueProvider valueProvider = controllerContext.Controller.ValueProvider;
- string modelName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
- Predicate<string> propertyFilter = ControllerActionInvoker.GetPropertyFilter(parameterDescriptor);
- ModelBindingContext bindingContext = new ModelBindingContext
- {
- FallbackToEmptyPrefix = parameterDescriptor.BindingInfo.Prefix == null,
- ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType),
- ModelName = modelName,
- ModelState = controllerContext.Controller.ViewData.ModelState,
- PropertyFilter = propertyFilter,
- ValueProvider = valueProvider
- };
- object obj = modelBinder.BindModel(controllerContext, bindingContext);
- return obj ?? parameterDescriptor.DefaultValue;
- }
- private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor)
- {
//action参数附带绑定信息了没?如果没有采用系统默认的方式进行绑定- return parameterDescriptor.BindingInfo.Binder ?? this.Binders.GetBinder(parameterDescriptor.ParameterType);
- }
- private IModelBinder GetBinder(Type modelType, IModelBinder fallbackBinder)
- {
- IModelBinder modelBinder = this._modelBinderProviders.GetBinder(modelType);
- if (modelBinder != null)
- {
- return modelBinder;
- }
- if (this._innerDictionary.TryGetValue(modelType, out modelBinder))
- {
- return modelBinder;
- }
- modelBinder = ModelBinders.GetBinderFromAttributes(modelType, () => string.Format(CultureInfo.CurrentCulture, MvcResources.ModelBinderDictionary_MultipleAttributes, new object[]
- {
- modelType.FullName
- }));
- return modelBinder ?? fallbackBinder;
- }
- private static ModelBinderDictionary CreateDefaultBinderDictionary()
- {
- return new ModelBinderDictionary
- {
- {
- typeof(HttpPostedFileBase),
- new HttpPostedFileBaseModelBinder()
- },
- {
- typeof(byte[]),
- new ByteArrayModelBinder()
- },
- {
- typeof(Binary),
- new LinqBinaryModelBinder()
- },
- {
- typeof(CancellationToken),
- new CancellationTokenModelBinder()
- }
- };
- }
下一篇: Action中的参数是怎么被赋值的
MVC中Action的执行过程的更多相关文章
- MVC中Action参数绑定的过程
一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二. ...
- 白话学习MVC(八)Action的执行二
一.概述 上篇博文<白话学习MVC(七)Action的执行一>介绍了ASP.NET MVC中Action的执行的简要流程,并且对TempData的运行机制进行了详细的分析,本篇来分析上一篇 ...
- Asp.net mvc 中Action 方法的执行(一)
[toc] 在 Aps.net mvc 应用中对请求的处理最终都是转换为对某个 Controller 中的某个 Action 方法的调用,因此,要对一个请求进行处理,第一步,需要根据请求解析出对应的 ...
- Asp.net mvc 中Action 方法的执行(三)
[toc] 前面介绍了 Action 方法执行过程中的一些主要的组件以及方法执行过程中需要的参数的源数据的提供以及参数的绑定,那些都可以看作是 Action 方法执行前的一些必要的准备工作,接下来便将 ...
- C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)
C# MVC 用户登录状态判断 来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...
- windows server 证书的颁发与IIS证书的使用 Dapper入门使用,代替你的DbSQLhelper Asp.Net MVC中Action跳转(转载)
windows server 证书的颁发与IIS证书的使用 最近工作业务要是用服务器证书验证,在这里记录下一. 1.添加服务器角色 [证书服务] 2.一路下一步直到证书服务安装完成; 3.选择圈选 ...
- 转:Oracle中SQL语句执行过程中
Oracle中SQL语句执行过程中,Oracle内部解析原理如下: 1.当一用户第一次提交一个SQL表达式时,Oracle会将这SQL进行Hard parse,这过程有点像程序编译,检查语法.表名.字 ...
- 游览器中javascript的执行过程
在讲这个问题之前,先来补充几个知识点,如果对此已经比较了解可以直接跳过 大多数游览器的组件构成如图 在最底层的三个组件分别是网络,UI后端和js解释器.作用如下: (1)网络- 用来完成网络调用,例如 ...
- Asp.net mvc 中Action 方法的执行(二)
[toc] 前面介绍了 Action 执行过程中的几个基本的组件,这里介绍 Action 方法的参数绑定. 数据来源 为 Action 方法提供参数绑定的原始数据来源于当前的 Http 请求,可能包含 ...
随机推荐
- [翻译]理解Ruby中的blocks,Procs和lambda
原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...
- 千万用户级别应用系统背后的SOA组件化容器
背景 在<我们的应用系统是如何支撑千万级别用户的>随笔中已经从“宏观”角度去介绍了整个应用系统的布局.组件化是整个系统由头到尾都始终坚持的一个设计原则,其中“SOA组件化容器”也是我们应用 ...
- 一个App完成入门篇(三)-完善主框架
本节教程将继续带领大家完善教学demo 导入项目 完善主框架 完成viewShower子视图 打开新页 启动动画 将要学习的demo效果图如下所示 1. 如何导入完整项目 本节示例demo请参考下载地 ...
- 源代码版本管理与项目管理软件的认识与github的注册
源代码版本管理软件: 主要有:svn,cvs,hg,git,VSS 这些工具主要是一种记录代码更改历史, 可以无限回溯, 用于代码管理,多个程序员开发协作的工具.Perforce,StarTeam)- ...
- FB引擎系列-之CloudSand
CloudSand,欲打破之前的集中版本制作的模式, http://code.taobao.org/p/cloudsand包含服务器端代码(php)和客户端代码(unity) EasyDown的时 ...
- 整理BOM时写的关于拆分单元格的VB代码
Public Function AddRows(pos As Integer, amount As Integer) Dim rpos As Integer rpos = pos + To amoun ...
- User and User Groups in Linux
本文梳理了一下Linux用户和用户组的常用的一些命令. 有关的配置文件: /etc/group 存储当前系统中所有用户组信息 /etc/gshadow 存储当前系统中所有用户组的密码 /etc/pas ...
- Netfilter/iptables的匹配方式及处理方法
匹配方式: 匹配方式是netfilter筛选数据包的最基本单元. 内置的匹配方式: 1.接口的匹配方式: iptables -t filter -A FORWARD -i eth0 -o eth1 - ...
- phpstudy80端口被占用时的解决方案
1.适合人群? 之前笔记本单独安装过Apache.php.mysql环境,但是后期想用集成开发环境phpstudy的,安装完phpstudy后(之前的单独环境依然存在),发现启动时,总是显示80端口被 ...
- 请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出
有些时候需要将二进制图片字节在发送浏览器以图片形式显示: 下面是一些示例代码: 控制器: /// <summary> /// 将图片的二进制字节字符串在视图页面以图片形式输出 /// &l ...