熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET MVC 4框架揭秘:Controller的激活與URL路由[2]

2022-06-13   來源: .NET編程 

  public class MvcHandler : IHttpAsyncHandler IHttpHandler IRequiresSessionState

  {

  //其他成員

  public RequestContext RequestContext { get; private set; }

  public bool IsReusable

  {

  get { return false; }

  }

  public MvcHandler(RequestContext requestContext)

  {

  thisRequestContext = requestContext;

  }

  IAsyncResult BeginProcessRequest(HttpContext context AsyncCallback cb

  object extraData)

  {

  IControllerFactory controllerFactory =

  ControllerBuilderCurrentGetControllerFactory()

  string controllerName =

  thisRequestContextRouteDataGetRequiredString(controller

  IController controller = controllerFactory

  CreateController(thisRequestContext controllerName)

  if (controller is IAsyncController)

  {

  try

  {

  //調用BeginExecute/EndExecute方法以異步的方式執行Controller

  }

  finally

  {

  controllerFactoryReleaseController(controller)

  }

  }

  else

  {

  try

  {

  //調用Execute方法以異步的方式執行Controller

  }

  finally

  {

  controllerFactoryReleaseController(controller)

  }

  }

  }

  }

  由於MvcHandler同時實現了IHttpHandler和IHttpAsyncHandler接口所以它總是以異步的方式被執行(調用BeginProcessRequest/EndProcessRequest方法)BeginProcessRequest方法通過RequestContext對象得到目標Controller的名稱然後利用當前ControllerBuilder創建的ControllerFactory激活Controller對象如果Controller類型實現了IAsyncController接口則以異步的方式執行Controller否則采用同步執行方式在被激活Controller對象被執行之後MvcHandler會調用ControllerFactory的ReleaseController對其進行釋放清理工作

       返回目錄ASPNET MVC 框架揭秘

       編輯推薦

       ASP NET開發培訓視頻教程

       Microsoft NET框架程序設計視頻教程

       Java程序性能優化讓你的Java程序更快更穩定

       Visual C++音頻/視頻技術開發與實戰

[]  []  


From:http://tw.wingwit.com/Article/program/net/201311/16087.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.