前面介绍了 Action 方法执行过程中的一些主要的组件以及方法执行过程中需要的参数的源数据的提供以及参数的绑定,那些都可以看作是 Action 方法执行前的一些必要的准备工作,接下来便将这些串起来看一下 Action 方法执行的整体的流程。

Action 执行的整体流程

  在接受到客户端的 Http 请求后,Asp.net 的路由系统会对请求信息进行解析得到路由数据,其中包括请求的 ControllerAction 的名称以及当前请求上下文的信息,然后根据这些信息创建对应的Controller 类型实例,调用请求 Action 所对应的 Action 方法对请求进行处理。

  在执行 Action 方法之前需要先执行定义在 Action 上的一些过滤器方法(如认证过滤器、授权过滤器等以及其它的需要在Action 方法执行前调用的过滤器方法),然后对当前要执行的 Action 方法的参数进行赋值,执行 Action 方法并根据执行结果生成 ActionResult 类型的结果,执行所有需要在 Action方法返回前要执行的过滤器方法,执行 ActionResult 并将结果返回给客户端。

  

  前面说过 Action 的执行是通过 IActionInvoker 类型来实现的,该接口中仅定义了一个 bool InvokeAction(ControllerContext context,string actionName) 方法,该方法的实现是通过同步的方式实现的,即具有一个实现类 ControllerActionInvoker,该接口还有一个异步实现的版本 IAsyncActionInvoker,该接口的定义如下示:

 public interface IAsyncActionInvoker : IActionInvoker
{
IAsyncResult BeginInvokeAction(ControllerContext controllerContext, string actionName, AsyncCallback callback, object state);
bool EndInvokeAction(IAsyncResult asyncResult);
}

  从上可以看出,其继承自 IActionInvoker 接口,该接口也具有一个实现类型 AsyncControllerActionInvoker,该接口同时实现了 IActionInvoker 接口 和 IAsyncActionInvoker 接口,同时还继承自 ControllerActionInvoker 类型。

ControllerActionInvoker

  该类型是 IActionInvoker 接口的唯一实现类型,其定义如下:

public class ControllerActionInvoker : IActionInvoker{

	//属性
protected internal ModelBinderDictionary Binders;//改属性值来自于静态类型 ModelBinders的同名属性 //方法 //根据 Action方法的执行结果创建 ActionResult
protected virtual ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue)
} // 获取当前执行的 Action 的描述类 ActionDescriptor
protected virtual ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName); //获取定义在当前 Action 方法上的过滤器信息
protected virtual FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor); //获取与指定 ParameterDescriptor 相关的的参数的值
protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor); //执行 actionName 对应的 Action方法
public virtual bool InvokeAction(ControllerContext controllerContext, string actionName); //执行 ActionResult
protected virtual void InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult); //执行 Action 上定义的 认证过滤器
protected virtual AuthenticationContext InvokeAuthenticationFilters(ControllerContext controllerContext,IList<IAuthenticationFilter> filters, ActionDescriptor actionDescriptor); //执行 Action 上定义的授权过滤器
protected virtual AuthorizationContext InvokeAuthorizationFilters(ControllerContext controllerContext, IList<IAuthorizationFilter> filters, ActionDescriptor actionDescriptor); //执行 Action 上定义的异常过滤器
protected virtual ExceptionContext InvokeExceptionFilters(ControllerContext controllerContext, IList<IExceptionFilter> filters, Exception exception);

  下面对几个比较关键的方法着重说明一下:

  其中最为重要的方法当属 InvokeAction,该方法是集中处理 Action 方法执行流程的地方,该方法的实现如下示:

public virtual bool InvokeAction(ControllerContext controllerContext, string actionName) {
//controllerContext 为 null,直接抛出异常
if (controllerContext == null) {
throw new ArgumentNullException("controllerContext");
} //actionName 为空且当前Action 没有使用直连路由(特性路由),抛出异常
if (String.IsNullOrEmpty(actionName) && !controllerContext.RouteData.HasDirectRouteMatch())
{
throw new AgumentException(MvcResources.Common_NullOrEmpty, "actionName");
} //定位匹配的 Action 方法
ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName); //找到了与请求匹配的 Action 方法
if (actionDescriptor != null) {
//获取定义在当前 Action 上的过滤器方法
FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor);
try {
//执行认证过滤器
AuthenticationContext authenticationContext = InvokeAuthenticationFilters(controllerContext, filterInfo.AuthenticationFilters, actionDescriptor);
if (authenticationContext.Result != null)
{
//在 IAuthenticationFilter 中定义了两个方法,OnAuthencation 和 OnAuthencationChallenge,其中第二个方法
//在 第一个方法后执行,但其具体的执行时机不是固定的,它可以在认证后执行,可以在授权后执行,亦可以在 action 方法执行后执行,下面的代码逻辑
//便体现了这一点
//http://www.dotnetcurry.com/aspnet-mvc/957/aspnet-mvc-authentication-filters
AuthenticationChallengeContext challengeContext = InvokeAuthenticationFiltersChallenge(
controllerContext,
filterInfo.AuthenticationFilters, actionDescriptor,authenticationContext.Result);
//执行授权过滤器和认证过滤器任一的验证结果的 ActionResult 不为 null,便会直接执行该 ActionResult,而不去进一步执行Action 方法
InvokeActionResult(controllerContext, challengeContext.Result ? ? authenticationContext.Result);
}
else {
AuthorizationContext authorizationContext = InvokeAuthorizationFilters(controllerContext, filterInfo.AuthorizationFilters, actionDescriptor);
if (authorizationContext.Result != null)
{
AuthenticationChallengeContext challengeContext = InvokeAuthenticationFiltersChallenge(controllerContext, filterInfo.AuthenticationFilters, actionDescriptor,
authorizationContext.Result);
InvokeActionResult(controllerContext, challengeContext.Result ? ? authorizationContext.Result);
}
else //走到这一步表示通过了认证过滤器和授权过滤器
{
//是否对请求的输入内容进行验证,如Html、JavaScript等内容不允许提交等
if (controllerContext.Controller.ValidateRequest)
{
ValidateRequest(controllerContext);
} //获取执行当前 Action所需的参数的值
IDictionary < string, object > parameters = GetParameterValues(controllerContext, actionDescriptor); ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters); //走到这一步,Action 方法已成功执行,针对一些特殊的认证过滤器,即使Action执行成功,仍然需要进行一些处理的
//例如针对不同的用户执行不同反馈操作
AuthenticationChallengeContext challengeContext = InvokeAuthenticationFiltersChallenge(
controllerContext, filterInfo.AuthenticationFilters, actionDescriptor,postActionContext.Result); InvokeActionResultWithFilters(controllerContext, filterInfo.ResultFilters,
challengeContext.Result ? ? postActionContext.Result);
}
}
} catch (ThreadAbortException) {
throw;
} catch (Exception ex) {
// 发生 Action 过程发生异常,执行异常过滤器
ExceptionContext exceptionContext = InvokeExceptionFilters(controllerContext, filterInfo.ExceptionFilters, ex);
if (!exceptionContext.ExceptionHandled)
{
throw;
}
InvokeActionResult(controllerContext, exceptionContext.Result);
}
return true;
}
//这里表示为找到与请求相匹配的 Action 方法
return false;
}

  我们知道,Action 方法的最终的返回结果一般都是一个 ActionResult 类型的实例,该实例便是根据下面的这个 InvokeActionMethod 方法来创建的,该方法的定义如下示:

protected virtual ActionResult InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters)
{//关于 Execute 方法的详细信息请参考 第一大节
object returnValue = actionDescriptor.Execute(controllerContext, parameters);
ActionResult result = CreateActionResult(controllerContext, actionDescriptor, returnValue);
return result;
}

  其中的 CreateActionResult 方法的实现逻辑特别的简单,就是如果参数 returnValue 为 null,则直接返回一个新的 EmptyResult 类型的对象,如果不为 null,就是用 as 运算符尝试将其转换为一个 ActionResult 类型对象,如果转换成功则返回,否则返回一个新的 ContentResult 类型对象,其中的内容为 returnValue 的字符串形式。在上面的 InvokeAction 方法中有多处调用了 InvokeActionWithFilters 方法,该方法的内部便是调用该方法创建的 ActionResult 类型的对象。

  执行 Action 方法编译生成的委托,根据委托的执行结果生成 ActionResult 类型的实例。至此,一个 Action 方法的执行便完成了,后续的步骤便是调用上一步生成的 ActionResult 实例的 ExecuteResult 方法,将执行的结果响应给客户端。

AsyncControllerActionInvoker

  AsyncControllerActionInvoker 的整体的处理逻辑与 ControllerActionInvoker 保持一致,其不同之处在于其 Action 方法的执行时采用的是异步执行的方式,例如,在前面的 ControllerDescriptorInvokeAction 方法中使用的 ControllerDescriptor 的类型实例是 ReflectedControllerDescriptorActionDescriptor 类型实例是 ReflectedActionDescriptor,而在该类型中使用的便是其一步的版本,ReflectedAsyncControllerDescriptorAsyncReflectedActionDescriptor

  

  至此,关于 Action 方法的执行,介绍完毕。

Asp.net mvc 中Action 方法的执行(三)的更多相关文章

  1. Asp.net mvc 中Action 方法的执行(二)

    [toc] 前面介绍了 Action 执行过程中的几个基本的组件,这里介绍 Action 方法的参数绑定. 数据来源 为 Action 方法提供参数绑定的原始数据来源于当前的 Http 请求,可能包含 ...

  2. Asp.net mvc 中Action 方法的执行(一)

    [toc] 在 Aps.net mvc 应用中对请求的处理最终都是转换为对某个 Controller 中的某个 Action 方法的调用,因此,要对一个请求进行处理,第一步,需要根据请求解析出对应的 ...

  3. 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 ...

  4. windows server 证书的颁发与IIS证书的使用 Dapper入门使用,代替你的DbSQLhelper Asp.Net MVC中Action跳转(转载)

    windows server 证书的颁发与IIS证书的使用   最近工作业务要是用服务器证书验证,在这里记录下一. 1.添加服务器角色 [证书服务] 2.一路下一步直到证书服务安装完成; 3.选择圈选 ...

  5. Asp.Net MVC中Action跳转(转载)

    首先action的跳转大致归类: 1跳转到与当前同一控制器内的action和不同控制器内的action. 2带有参数的action跳转和不带参数的action跳转. 3跳转到指定视图,不经过Contr ...

  6. Asp.Net MVC中Action跳转

    首先action的跳转大致归类: 1跳转到与当前同一控制器内的action和不同控制器内的action. 2带有参数的action跳转和不带参数的action跳转. 3跳转到指定视图,不经过Contr ...

  7. Asp.Net MVC中Action跳转小结

    首先我觉得action的跳转大致可以这样归一下类,跳转到同一控制器内的action和不同控制器内的action.带有参数的action跳转和不带参数的action跳转. 一.RedirectToAct ...

  8. asp.net mvc中action接收客户端发送过来的html片段

    出于安全的考虑,默认情况下,如果从客户端发送过来的数据中直接包括了HTML内容,ASP.NET会自动启动保护措施,这当然是一个比较好的设计,只不过在某种情况下我们真的需要获取这个值,那我们应该怎么办呢 ...

  9. asp.net mvc中动作方法的重定向

    简单介绍一下mvc控制器下怎样重定向的其它页面 1.方式1:Response.Redirect重定向 //Response.Redirect方式跳转 Response.Redirect("~ ...

随机推荐

  1. 房上的猫:while循环与do-while循环,debug的调试运用

    一.循环结构 1.循环不是无休止进行的,满足一定条件的时候循环才会继续,称为"循环条件",循环条件不满足的时候,循环退出 2.循环结构是反复进行相同的或类似的一系列操作,称为&qu ...

  2. 这是要逆天么,看我控制台程序玩Microsoft XPS Document 打印

    主要是想试试Microsoft XPS Document 打印时怎样去掉那个“将打印输出另存为”对话框 using System; using System.Drawing; using System ...

  3. chrome调试工具高级不完整使用指南(实战一)

    三.chrome调试工具实战 3.1 获取界面对应的HTML和修改样式 我们以博客园为例子来分析. 通过上面的操作就可以定位到对应的HTML代码 左侧菜单显示的就是当前指定元素层叠样式的一个情况 上面 ...

  4. 认证客户端的链接与socketserver实现并发

    from socket import * import hmac,os secret_key=b'linhaifeng bang bang bang' def conn_auth(conn): ''' ...

  5. 6、ABPZero系列教程之拼多多卖家工具 框架后台的设置

    接着上篇文章,现在去修改注册登录逻辑代码还为时过早,我们还需要到后台去设置一些配置. 管理---设置 先配置好这2项设置,邮箱配置是为了验证注册时功能是否正常,下一篇文章需要用到. 注:邮箱配置中的密 ...

  6. sync_binlog innodb_flush_log_at_trx_commit 浅析

    一 参数意义 innodb_flush_log_at_trx_commit 如果innodb_flush_log_at_trx_commit设置为0,log buffer将每秒一次地写入log fil ...

  7. 从Unity中的Attribute到AOP(四)

    本篇我们将逐一讲解Unity中经常使用的Attribute(Unity对应的文档版本为2018.1b). 首先是Serializable,SerializeField以及NonSerialized,H ...

  8. 12、ABPZero系列教程之拼多多卖家工具 拼团提醒功能登录拼多多实现

    上篇文章已经完成了整个拼多多拼团提醒功能,本篇继续完成拼多多帐号登录,拼多多帐号登录的目的是为了获取拼团商品的SKU和订单号,便于商家备货. 以下是拼多多官方的后台登录,要实现的功能并不是直接在这里登 ...

  9. 原生ajax jq跨域

    原生js封装ajax //创建XmlhttpRequest对象 function createXHR(){     var xhr=null;     if(XMLHttpRequest){      ...

  10. 认识Sass和Compass

    第一章 Sass和Compass让样式表重焕青春 // 内容概要// 开始学习Sass和动态样式表// 用Sass更高效地写样式表// Compass简介// 用Compass迎接工程实践中的样式挑战 ...