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

ASP.NET MVC 核心描述對象:ControllerDescriptor 與 ActionDescriptor 的建立機制

     編輯:關於ASP.NET

前言

ASP.NET MVC 中,ControllerDescriptor 與 ActionDescriptor 分別描述控制器與動作方法的元數據。它們各有同步與非同步兩個版本,那麼這些描述對象究竟是在何時、由誰建立的?本文將探討 ControllerActionInvoker 與 AsyncControllerActionInvoker 在此過程中的關鍵角色。

ActionInvoker:動作執行的核心組件

ActionInvoker 專責執行 Action 方法。Controller 基類擁有 ActionInvoker 屬性,預設用於該控制器所有 Action 的執行。介面定義如下:

public interface IActionInvoker
{
    bool InvokeAction(ControllerContext controllerContext, string actionName);
}

public abstract class Controller
{
    public IActionInvoker ActionInvoker { get; set; }
}

非同步版本 IAsyncActionInvoker 繼承自 IActionInvoker,並新增 BeginInvokeAction/EndInvokeAction 方法。

兩大實作類別

  • ControllerActionInvoker:同步執行 Action 的預設實作。
  • AsyncControllerActionInvoker:繼承自 ControllerActionInvoker,實作非同步介面,用於非同步 Action 執行。

兩者皆定義 GetControllerDescriptor 受保護方法,用於根據 ControllerContext 取得對應的 ControllerDescriptor。非同步版本覆寫了此方法。

預設 ActionInvoker 選擇機制

ASP.NET MVC 透過 DependencyResolver 依序嘗試解析:

  1. 以 IAsyncActionInvoker 介面解析,若非 null 則採用。
  2. 以 IActionInvoker 介面解析,若非 null 則採用。

若皆無註冊,則回退至內建預設實作。

總結

ControllerDescriptor 與 ActionDescriptor 的建立緊密綁定於 ActionInvoker 的生命週期。理解此機制有助於擴展自訂動作執行邏輯或整合依賴注入容器。

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