在本文中,我們將了解不同的事件,ASP.NET 應(yīng)用程序的生命周期以瀏覽器向 Web 服務(wù)器(對(duì)于 ASP.NET 應(yīng)用程序,通常為 IIS)發(fā)送請(qǐng)求為起點(diǎn),直至將請(qǐng)求結(jié)果返回至瀏覽器結(jié)束。在這個(gè)過程中,首先我們需要了解ASP.NET請(qǐng)求的2個(gè)大致的步驟。其次我們將詳細(xì)了解 'httphandler ',' httpmodule和 asp.net 頁面對(duì)象(Page)中不同的事件的執(zhí)行順序,邏輯。 二個(gè)步驟的過程: asp.net請(qǐng)求處理,2步的過程如下所示,用戶發(fā)送一個(gè)請(qǐng)求到IIS 服務(wù)器: 1、asp.net創(chuàng)建一個(gè)運(yùn)行時(shí),可以處理請(qǐng)求。換句話說,它創(chuàng)建應(yīng)用程序?qū)ο?,?qǐng)求,響應(yīng)和上下文對(duì)象處理請(qǐng)求。 2、運(yùn)行時(shí)一旦被創(chuàng)建,請(qǐng)求處理,通過一系列的事件處理模塊,Handler處理和頁面對(duì)象。簡稱MHPM (Module, handler, page and Module event)。
ASP.NET 應(yīng)用程序生命周期的各個(gè)階段: 步驟1:用戶從 Web 服務(wù)器請(qǐng)求應(yīng)用程序資源 ASP.NET 應(yīng)用程序的生命周期以瀏覽器向 Web 服務(wù)器(對(duì)于 ASP.NET 應(yīng)用程序,通常為 IIS)發(fā)送請(qǐng)求為起點(diǎn)。ASP.NET 是 Web 服務(wù)器下的 ISAPI 擴(kuò)展。Web 服務(wù)器接收到請(qǐng)求時(shí),會(huì)對(duì)所請(qǐng)求的文件的文件擴(kuò)展名進(jìn)行檢查,確定應(yīng)由哪個(gè) ISAPI 擴(kuò)展處理該請(qǐng)求,然后將該請(qǐng)求傳遞給合適的 ISAPI 擴(kuò)展。ASP.NET 處理已映射到其上的文件擴(kuò)展名,如 .aspx、.ascx、.ashx 和 .asmx。
步驟2:ASP.NET 接收對(duì)應(yīng)用程序的第一個(gè)請(qǐng)求 ApplicationManager creates an application domain." data-guid="56e45877ae1be994bf2684169708576f">當(dāng) ASP.NET 接收到對(duì)應(yīng)用程序中任何資源的第一個(gè)請(qǐng)求時(shí),名為 ApplicationManager 的類會(huì)創(chuàng)建一個(gè)應(yīng)用程序域。應(yīng)用程序域?yàn)槿肿兞刻峁?yīng)用程序隔離,并允許單獨(dú)卸載每個(gè)應(yīng)用程序。HostingEnvironment is created, which provides access to information about the application such as the name of the folder where the application is stored." data-guid="71b15fa47e4ac4001c67c6e50fbb3c2e">在應(yīng)用程序域中,將為名為 HostingEnvironment 的類創(chuàng)建一個(gè)實(shí)例,該實(shí)例提供對(duì)有關(guān)應(yīng)用程序的信息(如存儲(chǔ)該應(yīng)用程序的文件夾的名稱)的訪問。
步驟3:為每個(gè)請(qǐng)求創(chuàng)建 ASP.NET 核心對(duì)象 HostingEnvironment object instantiated, ASP.NET creates and initializes core objects such as HttpContext, HttpRequest, and HttpResponse." data-guid="b391ceb1f9ac2a56727f89a3251ab9f0">創(chuàng)建了應(yīng)用程序域并對(duì) HostingEnvironment 對(duì)象進(jìn)行了實(shí)例化之后,ASP.NET 將創(chuàng)建并初始化核心對(duì)象,如 HttpContext、HttpRequest 和 HttpResponse。HttpContext class contains objects that are specific to the current application request, such as the HttpRequest and HttpResponse objects." data-guid="1e9eccccb2f11debf339fcd22b5f169b">HttpContext 類包含特定于當(dāng)前應(yīng)用程序請(qǐng)求的對(duì)象,如 HttpRequest 和 HttpResponse 對(duì)象。HttpRequest object contains information about the current request, including cookies and browser information." data-guid="dab47d84b75e7f29cf8282f8b07b9b94">HttpRequest 對(duì)象包含有關(guān)當(dāng)前請(qǐng)求的信息,包括 Cookie 和瀏覽器信息。HttpResponse object contains the response that is sent to the client, including all rendered output and cookies." data-guid="2f065779f085181802f04f3222871d2a">HttpResponse 對(duì)象包含發(fā)送到客戶端的響應(yīng),包括所有呈現(xiàn)的輸出和 Cookie。
步驟4:HttpApplication object is assigned to the request" data-guid="6adc23d890f4a0ab707312a6c389b5af">將 HttpApplication 對(duì)象分配給請(qǐng)求HttpApplication object is assigned to the request" data-guid="6adc23d890f4a0ab707312a6c389b5af">HttpApplication class." data-guid="473f0898adf6e87cd39f4611f88a7108">初始化所有核心應(yīng)用程序?qū)ο笾?,將通過創(chuàng)建 HttpApplication 類的實(shí)例啟動(dòng)應(yīng)用程序。HttpApplication class and uses the derived class to represent the application." data-guid="303b1a8fce57c312b0dfa728c7b3d00d">如果應(yīng)用程序具有 Global.asax 文件,則 ASP.NET 會(huì)創(chuàng)建 Global.asax 類(從 HttpApplication 類派生)的一個(gè)實(shí)例,并使用該派生類表示應(yīng)用程序。 注:HttpApplication is created." data-guid="2f259bce31f998868df3b83ad01b7778">第一次在應(yīng)用程序中請(qǐng)求 ASP.NET 頁或進(jìn)程時(shí),將創(chuàng)建 HttpApplication 的一個(gè)新實(shí)例。HttpApplication instances might be reused for multiple requests." data-guid="abb9d7fdf05828fda39d6417a1c2c1d9">不過,為了盡可能提高性能,可對(duì)多個(gè)請(qǐng)求重復(fù)使用 HttpApplication 實(shí)例。
使用MHPM (Module, handler, page and Module event) 事件處理請(qǐng)求 1、如果你想通過*.aspx,*.html文件來處理程序邏輯,那么你需要使用 2、如果你想使用ASP.NET管道來處理程序邏輯,你需要使用
如何處理請(qǐng)求。mhpm有4個(gè)重要的步驟: 步驟1(M: HttpModule):客戶端開始請(qǐng)求處理。在asp.net引擎和
步驟2(H: ‘HttpHandler’):一旦上述6個(gè)事件被執(zhí)行完,asp.net引擎調(diào)用processrequest事件,如果已實(shí)現(xiàn)httphandler在您的項(xiàng)目。
步驟3(P: ASP.NET page):一旦
事件詳細(xì)介紹:
演示代碼: 在這段代碼中,我們創(chuàng)建HttpModule 和Httphandler 添加所有請(qǐng)求響應(yīng)的事件, 下面用 HttpModule Httphandler 跟蹤所有活動(dòng),并把它添加到一個(gè)全局性的集合變量中。
1 public class clsHttpModule : IHttpModule 2 { 3 private HttpApplication httpApp; 4 public static ArrayList objArrayList = new ArrayList(); 5 public clsHttpModule() 6 { 7 8 } 9 10 11 public void Dispose() 12 { 13 14 } 15 16 public void Init(HttpApplication context) 17 { 18 this.httpApp = context; 19 httpApp.Context.Response.Clear(); 20 objArrayList.Clear(); 21 objArrayList.Add("httpModule:Init"); 22 httpApp.AuthenticateRequest += new EventHandler(OnAuthentication); 23 httpApp.AuthorizeRequest += new EventHandler(OnAuthorization); 24 httpApp.BeginRequest += new EventHandler(OnBeginrequest); 25 httpApp.EndRequest += new EventHandler(OnEndRequest); 26 httpApp.ResolveRequestCache += new EventHandler(OnResolveRequestCache); 27 httpApp.AcquireRequestState += new EventHandler(OnAcquireRequestState); 28 httpApp.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute); 29 httpApp.PostRequestHandlerExecute += new EventHandler(OnPostRequestHandlerExecute); 30 httpApp.ReleaseRequestState += new EventHandler(OnReleaseRequestState); 31 httpApp.UpdateRequestCache += new EventHandler(OnUpdateRequestCache); 32 } 33 void OnUpdateRequestCache(object sender, EventArgs a) 34 { 35 objArrayList.Add("httpModule:OnUpdateRequestCache"); 36 } 37 void OnReleaseRequestState(object sender, EventArgs a) 38 { 39 objArrayList.Add("httpModule:OnReleaseRequestState"); 40 } 41 void OnPostRequestHandlerExecute(object sender, EventArgs a) 42 { 43 objArrayList.Add("httpModule:OnPostRequestHandlerExecute"); 44 } 45 void OnPreRequestHandlerExecute(object sender, EventArgs a) 46 { 47 objArrayList.Add("httpModule:OnPreRequestHandlerExecute"); 48 } 49 void OnAcquireRequestState(object sender, EventArgs a) 50 { 51 objArrayList.Add("httpModule:OnAcquireRequestState"); 52 } 53 void OnResolveRequestCache(object sender, EventArgs a) 54 { 55 objArrayList.Add("httpModule:OnResolveRequestCache"); 56 } 57 void OnAuthorization(object sender, EventArgs a) 58 { 59 objArrayList.Add("httpModule:OnAuthorization"); 60 } 61 void OnAuthentication(object sender, EventArgs a) 62 { 63 64 objArrayList.Add("httpModule:AuthenticateRequest"); 65 } 66 void OnBeginrequest(object sender, EventArgs a) 67 { 68 69 objArrayList.Add("httpModule:BeginRequest"); 70 } 71 void OnEndRequest(object sender, EventArgs a) 72 { 73 objArrayList.Add("httpModule:EndRequest"); 74 objArrayList.Add("<hr>"); 75 foreach (string str in objArrayList) 76 { 77 httpApp.Context.Response.Write(str + "<br>"); 78 } 79 80 } 81 82 } 以下代碼片段是跟蹤 1 public class clsHttpHandler : IHttpHandler 2 { 3 4 public bool IsReusable 5 { 6 get { return true; } 7 } 8 9 public void ProcessRequest(HttpContext context) 10 { 11 clsHttpModule.objArrayList.Add("HttpHandler:ProcessRequest"); 12 context.Response.Redirect("~/Default.aspx"); 13 } 14 } 頁面Page事件 1 public partial class _Default : System.Web.UI.Page 2 { 3 protected void Page_init(object sender, EventArgs e) 4 { 5 clsHttpModule.objArrayList.Add("Page:Init"); 6 } 7 protected void Page_Load(object sender, EventArgs e) 8 { 9 clsHttpModule.objArrayList.Add("Page:Load"); 10 } 11 public override void Validate() 12 { 13 clsHttpModule.objArrayList.Add("Page:Validate"); 14 } 15 protected void Button1_Click(object sender, EventArgs e) 16 { 17 clsHttpModule.objArrayList.Add("Page:Event"); 18 } 19 protected override void Render(HtmlTextWriter output) 20 { 21 clsHttpModule.objArrayList.Add("Page:Render"); 22 base.Render(output); 23 } 24 protected void Page_Unload(object sender, EventArgs e) 25 { 26 clsHttpModule.objArrayList.Add("Page:UnLoad"); 27 } 28 } webconfig中配置: ![]() 頁面第一次加載效果: 點(diǎn)擊button按鈕后: HttpModule HttpModule通過在某些事件中注冊(cè),把自己插入ASP.NET請(qǐng)求處理管道。當(dāng)這些事件發(fā)生的時(shí)候,ASP.NET調(diào)用對(duì)相應(yīng)的HTTP模塊,這樣該模塊就能處理請(qǐng)求了。 常用操作: 1、向每個(gè)頁面動(dòng)態(tài)添加一些備注或說明性的文字: 如果定義了多個(gè)HttpModule,在web.config文件中引入自定義HttpModule的順序就決定了多個(gè)自定義HttpModule在處理一個(gè)HTTP請(qǐng)求的接管順序。
HttpHandler 為了驗(yàn)證請(qǐng)求是否進(jìn)入HttpHandler ,我們可以在WebConfig中 加入: <add verb="*" path="*.aspx" type="WebDemo1.Common.clsHttpHandler,WebDemo1"/> 調(diào)試程序看看。
綜上我們可以總計(jì)出一張圖:
ASP.NET頁面事件 在上面的部分,我們已經(jīng)看到了一個(gè)ASP.NET頁面完整的請(qǐng)求事件。 最重要的部分之一,是ASP.NET頁面,我們沒有詳細(xì)討論相同。我們?cè)敿?xì)討論下ASP.NET的頁面事件。任何ASP.NET頁面中有2個(gè)部分 如圖: 注意 :大部分的開發(fā)者直接使用
|
|