接着上一篇:MVC控制器的激活过程

一、代码现行,该伪代码大致解析了Action的执行的过程

  1. try
  2. {
  3. Run each IAuthorizationFilter's OnAuthorization() method
  4.  
  5. if(none of the IAuthorizationFilters cancelled execution)
  6. {
  7. Run each IActionFilter's OnActionExecuting() method
  8. Run the action method
  9. Run each IActionFilter's OnActionExecuted() method (in reverse order)
  10.  
  11. Run each IResultFilter's OnResultExecuting() method
  12. Run the action result
  13. Run each IResultFilter's OnResultExecuted() method (in reverse order)
  14. }
  15. else
  16. {
  17. Run any action result set by the authorization filters
  18. }
  19. }
  20. catch(exception not handled by any action or result filter)
  21. {
  22. Run each IExceptionFilter's OnException() method
  23. Run any action result set by the exception filters
  24. }

二、返回主战场Action执行方法中

  1. public virtual bool InvokeAction(ControllerContext controllerContext, string actionName)
  2. {
  3. if (controllerContext == null)
  4. {
  5. throw new ArgumentNullException("controllerContext");
  6. }
  7. if (string.IsNullOrEmpty(actionName))
  8. {
  9. throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
  10. }
           //描述了控制器的相关信息
  11. ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(controllerContext);
    //描述了相关Action的相关信息
  12. ActionDescriptor actionDescriptor = this.FindAction(controllerContext, controllerDescriptor, actionName);
  13. if (actionDescriptor != null)
  14. {
    //获取该action的所有Filter
  15. FilterInfo filters = this.GetFilters(controllerContext, actionDescriptor);
  16. try
  17. {
  18. AuthorizationContext authorizationContext = this.InvokeAuthorizationFilters(controllerContext, filters.AuthorizationFilters, actionDescriptor);
  19. if (authorizationContext.Result != null)
  20. {
  21. this.InvokeActionResult(controllerContext, authorizationContext.Result);
  22. }
  23. else
  24. {
  25. if (controllerContext.Controller.ValidateRequest)
  26. {
    //地球人应该知道这个东西干嘛的,你知道吗?
  27. ControllerActionInvoker.ValidateRequest(controllerContext);
  28. }
  29. IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, actionDescriptor);
  30. ActionExecutedContext actionExecutedContext = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, actionDescriptor, parameterValues);
  31. this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, actionExecutedContext.Result);
  32. }
  33. }
  34. catch (ThreadAbortException)
  35. {
  36. throw;
  37. }
  38. catch (Exception exception)
  39. {
  40. ExceptionContext exceptionContext = this.InvokeExceptionFilters(controllerContext, filters.ExceptionFilters, exception);
  41. if (!exceptionContext.ExceptionHandled)
  42. {
  43. throw;
  44. }
  45. this.InvokeActionResult(controllerContext, exceptionContext.Result);
  46. }
  47. return true;
  48. }
  49. return false;
  50. }
  1. //上面的授权过程
    AuthorizationContext authorizationContext = this.InvokeAuthorizationFilters(controllerContext, filters.AuthorizationFilters, actionDescriptor);
  2. if (authorizationContext.Result != null)
  3. {
  4. this.InvokeActionResult(controllerContext, authorizationContext.Result);
  5. }
  1. protected virtual AuthorizationContext InvokeAuthorizationFilters(ControllerContext controllerContext, IList<IAuthorizationFilter> filters, ActionDescriptor actionDescriptor)
  2. {
  3. AuthorizationContext authorizationContext = new AuthorizationContext(controllerContext, actionDescriptor);
  4. foreach (IAuthorizationFilter current in filters)
  5. {
  6. current.OnAuthorization(authorizationContext);//很显然我们在自定义IAuthorizationFilter时,会去做事情在这里定义的。如果
      //此过程,authorizationContext的Result 被你给赋值了,那么所有的其他授权认证将会终止,系统将全力执行 this.InvokeActionResult(controllerContext, authorizationContext.Result);
  7. if (authorizationContext.Result != null)
  8. {
  9. break;
  10. }
  11. }
  12. return authorizationContext;
  13. }

三、Action连同过滤器的执行,上面谈了授权过滤器的执行

  1. IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, actionDescriptor);
  2. ActionExecutedContext actionExecutedContext = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, actionDescriptor, parameterValues);
  3. this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, actionExecutedContext.Result);
  1. protected virtual IDictionary<string, object> GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
  2. {
  3. Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  4. ParameterDescriptor[] parameters = actionDescriptor.GetParameters();
  5. ParameterDescriptor[] array = parameters;
  6. for (int i = ; i < array.Length; i++)
  7. {
  8. ParameterDescriptor parameterDescriptor = array[i];
  9. dictionary[parameterDescriptor.ParameterName] = this.GetParameterValue(controllerContext, parameterDescriptor);
  10. }
  11. return dictionary;
  12. }
  1. protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
  2. {
  3. Type parameterType = parameterDescriptor.ParameterType;
  4. IModelBinder modelBinder = this.GetModelBinder(parameterDescriptor);
  5. IValueProvider valueProvider = controllerContext.Controller.ValueProvider;
  6. string modelName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
  7. Predicate<string> propertyFilter = ControllerActionInvoker.GetPropertyFilter(parameterDescriptor);
  8. ModelBindingContext bindingContext = new ModelBindingContext
  9. {
  10. FallbackToEmptyPrefix = parameterDescriptor.BindingInfo.Prefix == null,
  11. ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType),
  12. ModelName = modelName,
  13. ModelState = controllerContext.Controller.ViewData.ModelState,
  14. PropertyFilter = propertyFilter,
  15. ValueProvider = valueProvider
  16. };
  17. object obj = modelBinder.BindModel(controllerContext, bindingContext);
  18. return obj ?? parameterDescriptor.DefaultValue;
  19. }
  1. private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor)
  2. {
    //action参数附带绑定信息了没?如果没有采用系统默认的方式进行绑定
  3. return parameterDescriptor.BindingInfo.Binder ?? this.Binders.GetBinder(parameterDescriptor.ParameterType);
  4. }
  1. private IModelBinder GetBinder(Type modelType, IModelBinder fallbackBinder)
  2. {
  3. IModelBinder modelBinder = this._modelBinderProviders.GetBinder(modelType);
  4. if (modelBinder != null)
  5. {
  6. return modelBinder;
  7. }
  8. if (this._innerDictionary.TryGetValue(modelType, out modelBinder))
  9. {
  10. return modelBinder;
  11. }
  12. modelBinder = ModelBinders.GetBinderFromAttributes(modelType, () => string.Format(CultureInfo.CurrentCulture, MvcResources.ModelBinderDictionary_MultipleAttributes, new object[]
  13. {
  14. modelType.FullName
  15. }));
  16. return modelBinder ?? fallbackBinder;
  17. }
  1. private static ModelBinderDictionary CreateDefaultBinderDictionary()
  2. {
  3. return new ModelBinderDictionary
  4. {
  5.  
  6. {
  7. typeof(HttpPostedFileBase),
  8. new HttpPostedFileBaseModelBinder()
  9. },
  10.  
  11. {
  12. typeof(byte[]),
  13. new ByteArrayModelBinder()
  14. },
  15.  
  16. {
  17. typeof(Binary),
  18. new LinqBinaryModelBinder()
  19. },
  20.  
  21. {
  22. typeof(CancellationToken),
  23. new CancellationTokenModelBinder()
  24. }
  25. };
  26. }

下一篇: Action中的参数是怎么被赋值的

MVC中Action的执行过程的更多相关文章

  1. MVC中Action参数绑定的过程

    一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二. ...

  2. 白话学习MVC(八)Action的执行二

    一.概述 上篇博文<白话学习MVC(七)Action的执行一>介绍了ASP.NET MVC中Action的执行的简要流程,并且对TempData的运行机制进行了详细的分析,本篇来分析上一篇 ...

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

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

  4. Asp.net mvc 中Action 方法的执行(三)

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

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

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

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

  7. 转:Oracle中SQL语句执行过程中

    Oracle中SQL语句执行过程中,Oracle内部解析原理如下: 1.当一用户第一次提交一个SQL表达式时,Oracle会将这SQL进行Hard parse,这过程有点像程序编译,检查语法.表名.字 ...

  8. 游览器中javascript的执行过程

    在讲这个问题之前,先来补充几个知识点,如果对此已经比较了解可以直接跳过 大多数游览器的组件构成如图 在最底层的三个组件分别是网络,UI后端和js解释器.作用如下: (1)网络- 用来完成网络调用,例如 ...

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

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

随机推荐

  1. [翻译]理解Ruby中的blocks,Procs和lambda

    原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...

  2. 千万用户级别应用系统背后的SOA组件化容器

    背景 在<我们的应用系统是如何支撑千万级别用户的>随笔中已经从“宏观”角度去介绍了整个应用系统的布局.组件化是整个系统由头到尾都始终坚持的一个设计原则,其中“SOA组件化容器”也是我们应用 ...

  3. 一个App完成入门篇(三)-完善主框架

    本节教程将继续带领大家完善教学demo 导入项目 完善主框架 完成viewShower子视图 打开新页 启动动画 将要学习的demo效果图如下所示 1. 如何导入完整项目 本节示例demo请参考下载地 ...

  4. 源代码版本管理与项目管理软件的认识与github的注册

    源代码版本管理软件: 主要有:svn,cvs,hg,git,VSS 这些工具主要是一种记录代码更改历史, 可以无限回溯, 用于代码管理,多个程序员开发协作的工具.Perforce,StarTeam)- ...

  5. FB引擎系列-之CloudSand

    CloudSand,欲打破之前的集中版本制作的模式, http://code.taobao.org/p/cloudsand包含服务器端代码(php)和客户端代码(unity)   EasyDown的时 ...

  6. 整理BOM时写的关于拆分单元格的VB代码

    Public Function AddRows(pos As Integer, amount As Integer) Dim rpos As Integer rpos = pos + To amoun ...

  7. User and User Groups in Linux

    本文梳理了一下Linux用户和用户组的常用的一些命令. 有关的配置文件: /etc/group 存储当前系统中所有用户组信息 /etc/gshadow 存储当前系统中所有用户组的密码 /etc/pas ...

  8. Netfilter/iptables的匹配方式及处理方法

    匹配方式: 匹配方式是netfilter筛选数据包的最基本单元. 内置的匹配方式: 1.接口的匹配方式: iptables -t filter -A FORWARD -i eth0 -o eth1 - ...

  9. phpstudy80端口被占用时的解决方案

    1.适合人群? 之前笔记本单独安装过Apache.php.mysql环境,但是后期想用集成开发环境phpstudy的,安装完phpstudy后(之前的单独环境依然存在),发现启动时,总是显示80端口被 ...

  10. 请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出

    有些时候需要将二进制图片字节在发送浏览器以图片形式显示: 下面是一些示例代码: 控制器: /// <summary> /// 将图片的二进制字节字符串在视图页面以图片形式输出 /// &l ...