ASP.NET MVC三个重要的描述对象:ControllerDescriptor

ASP.NET MVC应用的请求都是针对某个Controller的某个Action方法,所以对 请求的处理最终体现在对目标Action方法的执行。而Action方法具有相应的参数 ,所以在方法执行之前必须根据相应的规则从请求中提取相应的数据并将其转换 为Action方法参数列表,我们将这个过程称为Model绑定。在ASP.NET MVC应用编 程接口中,Action方法某个参数的元数据通过ParameterDescriptor表示,而两个 相关的类型ControllerDescriptor和ActionDescriptor则用于描述Controller和 Action方法。[本文已经同步到《How ASP.NET MVC Works?》中]

一、ControllerDescriptor

ControllerDescriptor包含了用于描述某个Controller的元数据信息。如下面 的代码片断所示,ControllerDescriptor具有三个属性,其中ControllerName和 ControllerType分别表示Controller的名称和类型,前者来源于路由信息;字符 串类型的UniqueId表示ControllerDescriptor的唯一标识,该标识由自身的类型 、Controller的类型以及Controller的名称三者派生。

   1: public abstract class ControllerDescriptor : ICustomAttributeProvider   2: {   3:      public virtual object[] GetCustomAttributes(bool inherit);   4:      public virtual object[] GetCustomAttributes(Type attributeType, bool inherit);   5:      public virtual bool IsDefined(Type attributeType, bool inherit);   6:      public virtual IEnumerable<FilterAttribute> GetFilterAttributes(bool useCache);   7:    8:      public abstract ActionDescriptor FindAction(ControllerContext controllerContext, string actionName);   9:      public abstract ActionDescriptor[] GetCanonicalActions();  10:  11:     public virtual string ControllerName { get; }  12:     public abstract Type ControllerType { get; }  13:     public virtual string UniqueId { get; }  14: }  15:   16: public interface ICustomAttributeProvider  17: {  18:     object[] GetCustomAttributes(bool inherit);  19:     object[] GetCustomAttributes(Type attributeType, bool inherit);  20:     bool IsDefined(Type attributeType, bool inherit);  21: }

ControllerDescriptor实现了ICustomAttributeProvider接口,意味着我们可 以通过调用GetCustomAttributes和GetCustomAttributes方法获取应用在 Controller类型上的所有自定义特性或者给定类型的特性,也可以调用IsDefined 方法判断指定的自定义特性类型是否应用在对应的Controller类型上。

另一个方法GetFilterAttributes用于获取应用在Controller上的所有筛选器 特性(继承自抽象类FilterAttribute)。筛选器是一种基于AOP的设计,它使我 们可以一些基于横切关注点相关逻辑的执行动态的注入到Action方法的执行前后 ,我们会在“Action方法的执行”中对筛选器进行详细地介绍。

ControllerDescriptor的FindAction方法根据指定的Controller上下文和名称 得到相应的Action方法,返回的是用于描述Action方法的ActionDescriptor对象 。而GetCanonicalActions得到当前Controller的所有Action方法,返回类型为 ActionDescriptor数组。

二、ReflectedControllerDescriptor

在ASP.NET MVC应用编程接口中定义了抽象类ControllerDescriptor的唯一继 承类型ReflectedControllerDescriptor。顾名思义, ReflectedControllerDescriptor通过反射的机制解析用于描述Controller的元数 据。如下面的代码片断所示,表示Controller类型的ControllerType属性在构造 函数中指定。ReflectedControllerDescriptor通过反射的方式获取应用在 Controller类型上的相关特性以提供针对ICustomAttributeProvider接口的实现 。

   1: public class ReflectedControllerDescriptor : ControllerDescriptor   2: {   3:     public ReflectedControllerDescriptor(Type controllerType);   4:   5:     public override object[] GetCustomAttributes(bool inherit);   6:     public override object[] GetCustomAttributes(Type attributeType, bool inherit);   7:     public override IEnumerable<FilterAttribute> GetFilterAttributes(bool useCache);   8:     public override bool IsDefined(Type attributeType, bool inherit);   9:   10:     public override ActionDescriptor FindAction( ControllerContext controllerContext, string actionName);  11:     public override ActionDescriptor[] GetCanonicalActions();  12:   13:     public sealed override Type ControllerType { get; }  14: }

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索action
, controller
, 方法
, 类型
, bool
, net 字符串 bool
, public
, IsDefined
, inherit()
bool类型数据
,以便于您获取更多的相关知识。

时间: 2024-09-02 15:39:38

ASP.NET MVC三个重要的描述对象:ControllerDescriptor的相关文章

ASP.NET MVC三个重要的描述对象:ControllerDescriptor和ActionDescriptor的创建

不论是用于描述Controller的ControllerDescriptor,还是用于描述Action方法的ActionDescriptor,都具有同步和异步两个版本,那么这些不同类型的ControllerDescriptor的ActionDescriptor是在什么情况下创建的呢? 一.ControllerActionInvoker与AsyncControllerActionInvoker ControllerDescriptor的创建设计到一个重要的名为ActionInvoker的组件,顾名

ASP.NET MVC三个重要的描述对象:ParameterDescriptor

Model绑定是为作为目标Action的方法准备参数列表的过程,所以针对参数的 描述才是Model绑定的核心.在ASP.NET MVC应用编程接口中,服务于Model绑定的 参数元数据通过ParameterDescriptor类型来表示,而ActionDescriptor的 GetParameters方法返回的就是一个ParameterDescriptor数组. 如下面的代码片断所示,ParameterDescriptor同样实现了 ICustomAttributeProvider接口提供应用在

ASP.NET MVC三个重要的描述对象:ActionDescriptor

在Model绑定过程中会通过激活的Controller类型创建用于描述它的 ControllerDescriptor对象.Controller是一组Action方法的集合,而每一个 Action通过ActionDescriptor对象来表示,在这篇文章中我们就来着重谈谈不同 类型的ActionDescriptor.[本文已经同步到<How ASP.NET MVC Works?>中] 一.ActionDescriptor 用于描述定义在Controller类中的Action方法的ActionDe

ASP.NET MVC三个重要的描述对象:ControllerDescriptor与ActionDescriptor

ControllerDescriptor与ActionDescriptor的创建 不论是用于描述Controller的ControllerDescriptor,还是用于描述Action方 法的ActionDescriptor,都具有同步和异步两个版本,那么这些不同类型的 ControllerDescriptor的ActionDescriptor是在什么情况下创建的呢? 一.ControllerActionInvoker与AsyncControllerActionInvoker Controlle

七天学会ASP.NET MVC (三)——ASP.Net MVC 数据处理

  第三天我们将学习Asp.Net中数据处理功能,了解数据访问层,EF,以及EF中常用的代码实现方式,创建数据访问层和数据入口,处理Post数据,以及数据验证等功能. 系列文章 七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)--ASP.NET MVC 数据传递 七天学会ASP.NET MVC (三)--ASP.Net MVC 数据处理 七天学会ASP.NET MVC (四)--用户授权认证问题 七天学会ASP.NET MVC (五

学习ASP.NET MVC(三) Controller/Action 深入解析与应用实例

一.摘要 一个Url请求经过了Routing处理后会调用Controller的Action方法. 中间的过程是怎样的? Action方 法中返回ActionResult对象后,如何到达View的? 本文将讲解Controller的基本用法, 深入分析 Controller的运行机制, 并且提供了创建所有类型Action的代码. 值得学习ASP.NET MVC时参考. 二.承上启下 在上一篇文章中, 我已经学会了如何使用Routing获取Controller和Action, 随后的程序会调用 Co

ASP.NET MVC的运行机制

一.ASP.NET + MVC IIS与ASP.NET管道 MVC.MVP以及Model2[上篇] MVC.MVP以及Model2[下篇] ASP.NET MVC是如何运行的[1]: 建立在"伪"MVC框架上的Web应用 ASP.NET MVC是如何运行的[2]: URL路由 ASP.NET MVC是如何运行的[3]: Controller的激活 ASP.NET MVC是如何运行的[4]: Action的执行 二.URL 路由 ASP.NET的路由系统:URL与物理文件的分离 ASP.

How ASP.NET MVC Works?

一.ASP.NET + MVC IIS与ASP.NET管道 MVC.MVP以及Model2[上篇] MVC.MVP以及Model2[下篇] ASP.NET MVC是如何运行的[1]: 建立在"伪"MVC框架上的Web应用 ASP.NET MVC是如何运行的[2]: URL路由 ASP.NET MVC是如何运行的[3]: Controller的激活 ASP.NET MVC是如何运行的[4]: Action的执行 二.URL 路由 ASP.NET的路由系统:URL与物理文件的分离 ASP.

ASP.NET MVC下的四种验证编程方式[续篇]

原文:ASP.NET MVC下的四种验证编程方式[续篇] 在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注ValidationAttribute特性"."让数据类型实现IValidatableObject或者IDataErrorInfo"),那么在ASP.NET MVC框架内部是如何提供针对这四种不同编程方式的支持的呢?接下来我们就来聊聊这背后的