自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解ASP.NET MVC應(yīng)用程序請(qǐng)求生命周期

開(kāi)發(fā) 后端
本文將介紹的是ASP.NET MVC應(yīng)用程序的請(qǐng)求生命周期,包括相應(yīng)的流程圖及步驟,希望能對(duì)大家有所幫助。

當(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)用程序的具體使用如下

  1. public class MvcApplication : System.Web.HttpApplication  
  2.   {  
  3.   public static void RegisterRoutes(RouteCollection routes)  
  4.   {  
  5.   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  6.  
  7.   routes.MapRoute(  
  8.   "Default",  // Route name  
  9.   "{controller}/{action}/{id}",   // URL with parameters  
  10.   new { controller = "Home"action = "Index"id = "" }  // Parameter defaults  
  11.   );  
  12.   routes.MapRoute(  
  13.  "Account",  // Route name  
  14.  "{controller}/{action}/{id}",   // URL with parameters  
  15.  new { controller = "Account"action = "LogOn"id = "" }  // Parameter defaults  
  16.  );  
  17.    
  18.   }  
  19.  
  20.   protected void Application_Start()  
  21.   {  
  22.   RegisterRoutes(RouteTable.Routes);  
  23.   }  
  24.   } 

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)如下:

  1. public class UrlRoutingModule : IHttpModule  
  2.   {  
  3.    // 主要的 Methods  
  4.    protected virtual void Init(HttpApplication application);  
  5.    private void OnApplicationPostMapRequestHandler(object sender, EventArgs e);  
  6.    private void OnApplicationPostResolveRequestCache(object sender, EventArgs e);  
  7.    public virtual void PostMapRequestHandler(HttpContextBase context);  
  8.    public virtual void PostResolveRequestCache(HttpContextBase context);  
  9.    void IHttpModule.Init(HttpApplication application);  
  10.  
  11.    // Properties  
  12.    public RouteCollection RouteCollection { get; set; }  
  13.  
  14.    }  
  15. UrlRoutingModule 在WebConfig的注冊(cè)  
  16.    <httpModules> 
  17. <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, 
  18. System.Web.Routing, Version=3.5.0.0, 
  19.  Culture=neutral,PublicKeyToken=31BF3856AD364E35"/> 
  20.    </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é)如下

  1. public interface IRouteHandler  
  2. {  
  3. // Methods  
  4. 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é)如下

  1.  public class MvcHandler : IHttpAsyncHandler, IHttpHandler, IRequiresSessionState  
  2. {  
  3. // 主要的Fields  
  4. private ControllerBuilder _controllerBuilder;  
  5. }  
  6. ControllerBuilder類主要有一個(gè)方法GetControllerFactory  
  7. public class ControllerBuilder  
  8. {  
  9.   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

【編輯推薦】

 

  1. 詳解ASP.NET MVC 2中的Area特性
  2. 專訪微軟MVP衣明志:走進(jìn)ASP.NET MVC 2框架開(kāi)發(fā)
  3. 詳解ASP.NET MVC 2中強(qiáng)類型HTML輔助方法
  4. 詳解ASP.NET MVC 2自定義驗(yàn)證
  5. Scott Gu教你使用ASP.NET MVC 2新功能
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2009-02-12 13:16:55

請(qǐng)求生命周期MVCASP.NET

2009-07-20 10:33:02

ASP.NET MVC

2009-07-23 18:14:17

MVC生命周期

2012-08-16 09:38:38

ASP.NET

2009-07-31 10:47:18

ASP.NET頁(yè)面生命

2009-08-10 14:31:46

ASP.NET組件設(shè)計(jì)ASP.NET控件生命

2009-07-23 13:26:21

2011-08-10 16:50:10

iPhone生命周期

2013-04-07 10:42:56

Asp.Net頁(yè)面周期

2009-07-23 10:23:44

2009-07-22 18:07:55

論壇應(yīng)用程序ASP.NET MVC

2009-07-24 11:25:53

Web應(yīng)用程序工程ASP.NET MVC

2009-07-22 18:02:27

論壇應(yīng)用程序ASP.NET MVC

2009-08-04 16:05:15

ASP.NET頁(yè)面生命

2009-08-04 16:50:15

ASP.NET頁(yè)面生命

2009-08-14 17:57:43

ASP.NET MVC

2015-07-09 15:42:48

ios應(yīng)用生命周期

2013-07-31 14:50:32

Windows PhoWP應(yīng)用程序生命周期

2009-08-03 14:18:40

ASP.NET編程模型ASP.NET頁(yè)面生命

2009-08-04 17:49:31

Web Page生命周ASP.NET Pos
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)