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

ASP.NET MVC 運作原理 (4):Action 執行流程

     編輯:關於ASP.NET

Controller 基類 ControllerBase 的 Execute 核心在於 Action 方法與 ActionResult 的執行,兩者皆由 ActionInvoker 組件負責。

ActionInvoker 介面與實作

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

public class ControllerActionInvoker : IActionInvoker
{
    public IModelBinder ModelBinder { get; private set; }
    public ControllerActionInvoker() { this.ModelBinder = new DefaultModelBinder(); }

    public void InvokeAction(ControllerContext controllerContext, string actionName)
    {
        MethodInfo method = controllerContext.Controller.GetType().GetMethods()
            .First(m => string.Compare(actionName, m.Name, true) == 0);
        List<object> parameters = new List<object>();
        foreach (ParameterInfo parameter in method.GetParameters())
        {
            parameters.Add(this.ModelBinder.BindModel(controllerContext, parameter.Name, parameter.ParameterType));
        }
        ActionResult actionResult = method.Invoke(controllerContext.Controller, parameters.ToArray()) as ActionResult;
        actionResult.ExecuteResult(controllerContext);
    }
}

ModelBinder 機制

IModelBinder 介面定義 BindModel 方法,預設實作 DefaultModelBinder 負責從請求資料綁定 Action 參數。

public interface IModelBinder
{
    object BindModel(ControllerContext controllerContext, string modelName, Type modelType);
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved