Welcome 微信登录

首页 / 网页编程 / ASP.NET / ASP.NET MVC三个重要的描述对象:ControllerDescriptor

ASP.NET MVC三个重要的描述对象:ControllerDescriptor2012-08-28 博客园 ArtechASP.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: }