詳解ASP.NET MVC應(yīng)用程序請(qǐng)求生命周期
當(dāng)一個(gè)ASP.NET MVC應(yīng)用程序提出請(qǐng)求,為了響應(yīng)請(qǐng)求,包含一些請(qǐng)求執(zhí)行流程步驟! 在ASP.NET MVC應(yīng)用程序Http request
和Http response 過(guò)程中,主要包含8個(gè)步驟:
1)RouteTable(路由表)的創(chuàng)建
2)UrlRoutingModule 請(qǐng)求攔截
3)Routing engine 確定route
4)route handler 創(chuàng)建相關(guān)的IHttpHandler實(shí)例
5)IHttpHandler實(shí)例確定Controller(控制器)
6)Controller執(zhí)行
7)一個(gè)視圖引擎創(chuàng)建
8) 視圖呈現(xiàn)
主要流程圖如下:
1)RouteTable的創(chuàng)建
RouteTable的創(chuàng)建發(fā)生在mvc應(yīng)用程序的啟動(dòng) 或者web應(yīng)用程序池的重啟!通常的asp.net程序,一個(gè)頁(yè)面請(qǐng)求對(duì)應(yīng)磁盤上的一個(gè)頁(yè)面!如(http://localhost/index.aspx
對(duì)應(yīng)到服務(wù)器磁盤上的文件index.aspx)index.aspx實(shí)際上是一個(gè)類,由IHttpHandler創(chuàng)建實(shí)例化。IHttpHandler包含一個(gè)
ProcessRequest方法,負(fù)責(zé)響應(yīng)頁(yè)面輸出!
但是mvc application 是不同的,每一個(gè)請(qǐng)求映射到route,route 定義在route table,在應(yīng)用程序啟動(dòng)時(shí)創(chuàng)建!
RouteTable的在應(yīng)用程序的具體使用如下
- public class MvcApplication : System.Web.HttpApplication
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- "Default", // Route name
- "{controller}/{action}/{id}", // URL with parameters
- new { controller = "Home", action = "Index", id = "" } // Parameter defaults
- );
- routes.MapRoute(
- "Account", // Route name
- "{controller}/{action}/{id}", // URL with parameters
- new { controller = "Account", action = "LogOn", id = "" } // Parameter defaults
- );
- }
- protected void Application_Start()
- {
- RegisterRoutes(RouteTable.Routes);
- }
- }
2)UrlRoutingModule 請(qǐng)求攔截
每一個(gè)Http 請(qǐng)求 都被UrlRoutingModule攔截,UrlRoutingModule提供了當(dāng)前的HttpContext的routing engine(路由引擎)。HttpContext實(shí)例包含當(dāng)前請(qǐng)求的所有數(shù)據(jù)。UrlRoutingModule控制著routing engine,提供了HttpContext數(shù)據(jù)到routing engine! UrlRoutingModule實(shí)現(xiàn)了IHttpModule接口,在web.config 文件進(jìn)行了注冊(cè)!
UrlRoutingModule 具體的數(shù)據(jù)結(jié)構(gòu)如下:
- public class UrlRoutingModule : IHttpModule
- {
- // 主要的 Methods
- protected virtual void Init(HttpApplication application);
- private void OnApplicationPostMapRequestHandler(object sender, EventArgs e);
- private void OnApplicationPostResolveRequestCache(object sender, EventArgs e);
- public virtual void PostMapRequestHandler(HttpContextBase context);
- public virtual void PostResolveRequestCache(HttpContextBase context);
- void IHttpModule.Init(HttpApplication application);
- // Properties
- public RouteCollection RouteCollection { get; set; }
- }
- UrlRoutingModule 在WebConfig的注冊(cè)
- <httpModules>
- <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
- System.Web.Routing, Version=3.5.0.0,
- Culture=neutral,PublicKeyToken=31BF3856AD364E35"/>
- </httpModules>
3)Routing engine 確定route
routing engine基于當(dāng)前HttpContext確定Route的處理。routing engine 指出route table里面匹配的route ,并在IRouteHandler實(shí)例創(chuàng)建route處理!
4)route handler 創(chuàng)建相關(guān)的IHttpHandler實(shí)例在route table里,每一個(gè)route 都與一個(gè)IHttpHandler對(duì)應(yīng)。IHttpHandler基于當(dāng)前的HttpContext數(shù)據(jù)負(fù)責(zé)創(chuàng)建一個(gè)Controller(控制器)!IHttpHandler是由當(dāng)前活動(dòng)的IRouteHandler的GetHttpHandler所創(chuàng)建!
具體的細(xì)節(jié)如下
- public interface IRouteHandler
- {
- // Methods
- IHttpHandler GetHttpHandler(RequestContext requestContext);
- }
5)IHttpHandler實(shí)例確定Controller(控制器)
在MVC應(yīng)用程序中,MvcHandler實(shí)現(xiàn)了IHttpHandler,Controller實(shí)例,是基于所輸入的HttpContext 和Url參數(shù)與route 對(duì)應(yīng)的,ControllerFactory 創(chuàng)建一個(gè)controller,ControllerContext包含上下文數(shù)據(jù),傳入到controller的Excute方法,觸發(fā)controller的邏輯處理!
MvcHandler主要有一個(gè)ControllerBuilder _controllerBuilder字段;
具體細(xì)節(jié)如下
- public class MvcHandler : IHttpAsyncHandler, IHttpHandler, IRequiresSessionState
- {
- // 主要的Fields
- private ControllerBuilder _controllerBuilder;
- }
- ControllerBuilder類主要有一個(gè)方法GetControllerFactory
- public class ControllerBuilder
- {
- public IControllerFactory GetControllerFactory();
- }
通過(guò)實(shí)現(xiàn)IControllerFactory 工廠 創(chuàng)建一個(gè)Controller
6)Controller執(zhí)行
所有的controller 邏輯調(diào)用執(zhí)行時(shí),actions請(qǐng)求被執(zhí)行!當(dāng)controller的邏輯被執(zhí)行時(shí),會(huì)返回一個(gè)ActionResult。一個(gè)ActionResult實(shí)例,會(huì)觸發(fā)呈現(xiàn)一個(gè)View(視圖),當(dāng)觸發(fā)發(fā)生時(shí),一個(gè)視圖引擎被創(chuàng)建,進(jìn)行進(jìn)一步的處理
7)一個(gè)視圖引擎創(chuàng)建
視圖引擎實(shí)例會(huì)創(chuàng)建一個(gè)IView接口實(shí)例,返回一個(gè)ViewEngineResult實(shí)例,
8) 視圖呈現(xiàn)
IView實(shí)例編譯請(qǐng)求視圖,提供Render方法調(diào)用的數(shù)據(jù)!
原文標(biāo)題:MVC 請(qǐng)求生命周期
鏈接:http://www.cnblogs.com/skyyang/archive/2010/04/02/1702769.html
【編輯推薦】