日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

ASP.NET應(yīng)用程序與頁面生命周期

 細(xì)想生活 2015-09-17

      在本文中,我們將了解不同的事件,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、HttpRequestHttpResponse。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ì)象,如 HttpRequestHttpResponse 對(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í)例。

 

步驟5: HttpApplication pipeline." data-guid="85c644465ca255f10b2b21d8615ad419">由 HttpApplication 管線處理請(qǐng)求 MHPM (Module, handler, page and Module event)HttpApplication pipeline." data-guid="85c644465ca255f10b2b21d8615ad419">。

 

 

 

 

 

 

使用MHPM (Module, handler, page and Module event) 事件處理請(qǐng)求 
一旦 HttpApplication對(duì)象創(chuàng)建,HttpApplication class while the request is processed." data-guid="b0d256df234011acc1d1feeb566094ce">在處理該請(qǐng)求時(shí)將由 HttpApplication 類執(zhí)行。我們來了解下HttpModule HttpHandlers。

1、如果你想通過*.aspx,*.html文件來處理程序邏輯,那么你需要使用HttpHandler,換句話說,httphandler是一個(gè)擴(kuò)展的處理器。

2、如果你想使用ASP.NET管道來處理程序邏輯,你需要使用HttpModule 換句話說,httpmodule是一個(gè)事件處理器。

 

如何處理請(qǐng)求。mhpm有4個(gè)重要的步驟:

步驟1(M: HttpModule):客戶端開始請(qǐng)求處理。在asp.net引擎和httpmodule中的事件可以用來處理用戶自己定義的邏輯。有6個(gè)重要的事件,你可以在你的頁面對(duì)象被創(chuàng)建之前調(diào)用 Begin Requestauthenticaterequest,authorizerequestresolverequestcache,acquirerequeststateprerequesthandlerexecute。

 

步驟2(H: ‘HttpHandler’):一旦上述6個(gè)事件被執(zhí)行完,asp.net引擎調(diào)用processrequest事件,如果已實(shí)現(xiàn)httphandler在您的項(xiàng)目。

 

步驟3(P: ASP.NET page):一旦httphandler執(zhí)行,asp.net頁面對(duì)象被創(chuàng)建。而asp.net頁面對(duì)象被創(chuàng)建,Page對(duì)象的事件將被調(diào)用,這可以幫助我們頁面中定制自定義邏輯的處理。有6個(gè)重要的事件,Init, Load, validate, event, render ,unload 簡稱 SILVER   S – Start (does not signify anything as such just forms the word) , I – (Init) , L (Load) , V (Validate), E (Event) and R (Render)。

步驟4(M: HttpModule):一旦頁面對(duì)象是執(zhí)行和從內(nèi)存中卸載,httpmodule提供網(wǎng)頁執(zhí)行事件可以用來注入自定義處理邏輯。有4個(gè)重要的后處理事件postrequesthandlerexecute,releaserequeststateupdaterequestcacheendrequest。

 

 

 事件詳細(xì)介紹:

 
部分 Event事件 Description描述
HttpModule BeginRequest 在 ASP.NET 響應(yīng)請(qǐng)求時(shí)作為 HTTP 執(zhí)行管線鏈中的第一個(gè)事件發(fā)生。BeginRequest 事件發(fā)出信號(hào)表示創(chuàng)建任何給定的新請(qǐng)求。此事件始終被引發(fā),并且始終是請(qǐng)求處理期間發(fā)生的第一個(gè)事件。
HttpModule AuthenticateRequest 當(dāng)安全模塊已建立用戶標(biāo)識(shí)時(shí)發(fā)生。AuthenticateRequest 事件發(fā)出信號(hào)表示配置的身份驗(yàn)證機(jī)制已對(duì)當(dāng)前請(qǐng)求進(jìn)行了身份驗(yàn)證。
HttpModule AuthorizeRequest 安全模塊已驗(yàn)證用戶授權(quán)時(shí)發(fā)生。AuthorizeRequest 事件發(fā)出信號(hào)表示 ASP.NET 已對(duì)當(dāng)前請(qǐng)求進(jìn)行了授權(quán)。
HttpModule ResolveRequestCache 在 ASP.NET 完成授權(quán)事件以使緩存模塊從緩存中為請(qǐng)求提供服務(wù)后發(fā)生,從而繞過事件處理程序(例如某個(gè)頁或 XML Web services)的執(zhí)行。
HttpModule AcquireRequestState 當(dāng) ASP.NET 獲取與當(dāng)前請(qǐng)求關(guān)聯(lián)的當(dāng)前狀態(tài)(如會(huì)話狀態(tài))時(shí)發(fā)生。AcquireRequestState 事件在創(chuàng)建了事件處理程序之后引發(fā)。
HttpModule PreRequestHandlerExecute 恰好在 ASP.NET 開始執(zhí)行事件處理程序(例如,某頁或某個(gè) XML Web services)前發(fā)生。
HttpHandler ProcessRequest Httphandler邏輯被執(zhí)行。 在本節(jié)中,我們會(huì)寫,需要將每頁擴(kuò)展執(zhí)行的邏輯。
Page Init

OnInit 方法執(zhí)行創(chuàng)建 Page 實(shí)例所需的初始化和設(shè)置步驟。在頁生命周期的此階段中,頁中聲明的服務(wù)器控件都已初始化為默認(rèn)狀態(tài);但每個(gè)控件的視圖狀態(tài)尚未填充。Page_Init phase, regardless of whether the other controls are child or parent controls." data-guid="38a63102805c00854156e82b6502cc68">在 Page_Init 階段中,頁中的控件不能訪問該頁的其他服務(wù)器控件,無論其他控件是子控件還是父控件。不一定會(huì)創(chuàng)建其他服務(wù)器控件,也不一定能夠訪問它們

Page Load ASP.NET控件完成加載,你寫的UI操作邏輯或任何其他邏輯在這里。
Page Validate 如果你有你的頁面上有驗(yàn)證邏輯,這里檢查是否符合驗(yàn)證。
  Render 將頁面最終的輸出發(fā)送到瀏覽器,如果你想做出一些最終的HTML的變化,這是輸出到瀏覽器,你可以在這里輸入你的HTML的邏輯。
Page Unload 頁面對(duì)象從內(nèi)存中卸載。
HttpModule PostRequestHandlerExecute 在 ASP.NET 事件處理程序(例如,某頁或某個(gè) XML Web service)執(zhí)行完畢時(shí)發(fā)生。
HttpModule ReleaserequestState 在 ASP.NET 執(zhí)行完所有請(qǐng)求事件處理程序后發(fā)生。該事件將使?fàn)顟B(tài)模塊保存當(dāng)前狀態(tài)數(shù)據(jù)。ReleaseRequestState event is raised, the application is finished with the request and ASP.NET is signaled to store the request state." data-guid="c9d4b9283b2730849f0e239408395b2f">引發(fā) ReleaseRequestState 事件以后,應(yīng)用程序以該請(qǐng)求結(jié)束,并發(fā)出 ASP.NET 信號(hào)以存儲(chǔ)該請(qǐng)求狀態(tài)。
HttpModule UpdateRequestCache Before you end, if you want to update your cache.結(jié)束之前,如果您想更新您的高速緩存。
HttpModule EndRequest 當(dāng) ASP.NET 執(zhí)行完事件處理程序以使緩存模塊存儲(chǔ)將用于從緩存為后續(xù)請(qǐng)求提供服務(wù)的響應(yīng)時(shí)發(fā)生。

 

演示代碼:

  在這段代碼中,我們創(chuàng)建HttpModule 和Httphandler  添加所有請(qǐng)求響應(yīng)的事件, 下面用 HttpModule Httphandler  跟蹤所有活動(dòng),并把它添加到一個(gè)全局性的集合變量中。

 

復(fù)制代碼
 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     }
復(fù)制代碼

以下代碼片段是跟蹤 HttpHandlerProcessRequest '事件' :

復(fù)制代碼
 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     }
復(fù)制代碼

頁面Page事件

復(fù)制代碼
 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     }
復(fù)制代碼

 webconfig中配置:

HttpModules配置

頁面第一次加載效果:

點(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)添加一些備注或說明性的文字:
2、判斷用戶登錄

如果定義了多個(gè)HttpModule,在web.config文件中引入自定義HttpModule的順序就決定了多個(gè)自定義HttpModule在處理一個(gè)HTTP請(qǐng)求的接管順序。

 

HttpHandler
HttpHandler是HTTP請(qǐng)求的處理中心,真正地對(duì)客戶端請(qǐng)求的服務(wù)器頁面做出編譯和執(zhí)行,并將處理過后的信息附加在HTTP請(qǐng)求信息流中再次返回到HttpModule中。
HttpHandler與HttpModule不同,一旦定義了自己的HttpHandler類,那么它對(duì)系統(tǒng)的HttpHandler的關(guān)系將是“覆蓋”關(guān)系。

為了驗(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ā)者直接使用page_load方法的一切,這不是一個(gè)好的做法。例如 填充控件,設(shè)置視圖狀態(tài),應(yīng)用主題等,這些都是發(fā)生在頁面上的負(fù)荷。因此,我們可以在適當(dāng)?shù)氖录砑舆m當(dāng)?shù)倪壿?,這將真正使你的代碼整潔 符合邏輯。

 
序號(hào) 事件  控件初始化 狀態(tài) 是否
可用
表單數(shù)據(jù)是否
可用
什么邏輯可以寫在這里?
1 Init No No No

OnInit 方法執(zhí)行創(chuàng)建 Page 實(shí)例所需的初始化和設(shè)置步驟。在頁生命周期的此階段中,頁中聲明的服務(wù)器控件都已初始化為默認(rèn)狀態(tài);但每個(gè)控件的視圖狀態(tài)尚未填充。Page_Init phase, regardless of whether the other controls are child or parent controls." data-guid="38a63102805c00854156e82b6502cc68">在 Page_Init 階段中,頁中的控件不能訪問該頁的其他服務(wù)器控件,無論其他控件是子控件還是父控件。不一定會(huì)創(chuàng)建其他服務(wù)器控件,也不一定能夠訪問它們。

2  Load view state Not guaranteed Yes Not guaranteed 您可以訪問視圖狀態(tài)和任何同步邏輯
3 PostBackdata Not guaranteed Yes Yes  你可以訪問表單數(shù)據(jù)。
4  Load Yes Yes Yes 綁定控件 等等。。。
5  Validate Yes Yes Yes 如果你的頁面有驗(yàn)證,或者你想為您的網(wǎng)頁執(zhí)行驗(yàn)證。
6 Event Yes Yes Yes 如果這是通過點(diǎn)擊一個(gè)按鈕或下拉的變化,那么相對(duì)應(yīng)的事件將被執(zhí)行。
7  Pre-render Yes Yes Yes 如果你想在這些控件視圖狀態(tài)最后保存之前修改UI對(duì)象的結(jié)構(gòu)或?qū)傩灾怠?
8 Save view state Yes Yes Yes 一旦所有服務(wù)器控件的變化完成,這一事件控制數(shù)據(jù)保存在視圖狀態(tài)。
9  Render Yes Yes Yes 如果你想添加一些自定義的HTML輸出。
10  Unload Yes Yes Yes

你可以在這里做任何清理。

 

 

 

 

 

 

 

 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多