首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET MVC 核心描述對象:ControllerDescriptor 詳細解析

     編輯:關於ASP.NET

前言

ASP.NET MVC 請求處理最終落實於 Action 方法執行,而 Action 參數的準備依賴模型繫結。ControllerDescriptor、ActionDescriptor、ParameterDescriptor 三大描述對象構成了此流程的元數據骨架。本文聚焦 ControllerDescriptor 及其唯一實作 ReflectedControllerDescriptor。

ControllerDescriptor 核心成員

public abstract class ControllerDescriptor : ICustomAttributeProvider
{
    public virtual object[] GetCustomAttributes(bool inherit);
    public virtual object[] GetCustomAttributes(Type attributeType, bool inherit);
    public virtual bool IsDefined(Type attributeType, bool inherit);
    public virtual IEnumerable<FilterAttribute> GetFilterAttributes(bool useCache);

    public abstract ActionDescriptor FindAction(ControllerContext controllerContext, string actionName);
    public abstract ActionDescriptor[] GetCanonicalActions();

    public virtual string ControllerName { get; }
    public abstract Type ControllerType { get; }
    public virtual string UniqueId { get; }
}
  • ControllerName:來源於路由資訊。
  • ControllerType:控制器的 CLR 型別。
  • UniqueId:由描述器型別、控制器型別、控制器名稱衍生的唯一識別碼。
  • FindAction / GetCanonicalActions:依名稱或取得所有 Action 描述器。
  • GetFilterAttributes:取得套用於控制器的篩選器屬性(AOP 横切關注點)。

ReflectedControllerDescriptor:反射實作

ASP.NET MVC 僅提供此具體實作,透過反射解析控制器元數據:

public class ReflectedControllerDescriptor : ControllerDescriptor
{
    public ReflectedControllerDescriptor(Type controllerType);

    public override object[] GetCustomAttributes(bool inherit);
    public override object[] GetCustomAttributes(Type attributeType, bool inherit);
    public override IEnumerable<FilterAttribute> GetFilterAttributes(bool useCache);
    public override bool IsDefined(Type attributeType, bool inherit);

    public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName);
    public override ActionDescriptor[] GetCanonicalActions();

    public sealed override Type ControllerType { get; }
}

建構子接收 Type controllerType,內部利用反射快取屬性與方法資訊,效能經優化。

實務意義

理解 ControllerDescriptor 有助於開發自訂動作選擇器、篩選器提供者,或建構動態控制器發現機制。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved