乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       細想生活 2015-09-17

            在本文中,我們將了解不同的事件,ASP.NET 應(yīng)用程序的生命周期以瀏覽器向 Web 服務(wù)器(對于 ASP.NET 應(yīng)用程序,通常為 IIS)發(fā)送請求為起點,直至將請求結(jié)果返回至瀏覽器結(jié)束。在這個過程中,首先我們需要了解ASP.NET請求的2個大致的步驟。其次我們將詳細了解 'httphandler ',' httpmodule和 asp.net 頁面對象(Page)中不同的事件的執(zhí)行順序,邏輯。

      二個步驟的過程:

      asp.net請求處理,2步的過程如下所示,用戶發(fā)送一個請求到IIS 服務(wù)器:

      1、asp.net創(chuàng)建一個運行時,可以處理請求。換句話說,它創(chuàng)建應(yīng)用程序?qū)ο螅埱?,響?yīng)和上下文對象處理請求。

      2、運行時一旦被創(chuàng)建,請求處理,通過一系列的事件處理模塊,Handler處理和頁面對象。簡稱MHPM (Module, handler, page and Module event)。

       

       

      ASP.NET 應(yīng)用程序生命周期的各個階段:

      步驟1:用戶從 Web 服務(wù)器請求應(yīng)用程序資源 ASP.NET 應(yīng)用程序的生命周期以瀏覽器向 Web 服務(wù)器(對于 ASP.NET 應(yīng)用程序,通常為 IIS)發(fā)送請求為起點。ASP.NET 是 Web 服務(wù)器下的 ISAPI 擴展。Web 服務(wù)器接收到請求時,會對所請求的文件的文件擴展名進行檢查,確定應(yīng)由哪個 ISAPI 擴展處理該請求,然后將該請求傳遞給合適的 ISAPI 擴展。ASP.NET 處理已映射到其上的文件擴展名,如 .aspx、.ascx、.ashx 和 .asmx

       

      步驟2:ASP.NET 接收對應(yīng)用程序的第一個請求 ApplicationManager creates an application domain." data-guid="56e45877ae1be994bf2684169708576f">當 ASP.NET 接收到對應(yīng)用程序中任何資源的第一個請求時,名為 ApplicationManager 的類會創(chuàng)建一個應(yīng)用程序域。應(yīng)用程序域為全局變量提供應(yīng)用程序隔離,并允許單獨卸載每個應(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)建一個實例,該實例提供對有關(guān)應(yīng)用程序的信息(如存儲該應(yīng)用程序的文件夾的名稱)的訪問。

       

       

       

      步驟3:為每個請求創(chuàng)建 ASP.NET 核心對象 HostingEnvironment object instantiated, ASP.NET creates and initializes core objects such as HttpContext, HttpRequest, and HttpResponse." data-guid="b391ceb1f9ac2a56727f89a3251ab9f0">創(chuàng)建了應(yīng)用程序域并對 HostingEnvironment 對象進行了實例化之后,ASP.NET 將創(chuàng)建并初始化核心對象,如 HttpContextHttpRequestHttpResponse。HttpContext class contains objects that are specific to the current application request, such as the HttpRequest and HttpResponse objects." data-guid="1e9eccccb2f11debf339fcd22b5f169b">HttpContext 類包含特定于當前應(yīng)用程序請求的對象,如 HttpRequestHttpResponse 對象。HttpRequest object contains information about the current request, including cookies and browser information." data-guid="dab47d84b75e7f29cf8282f8b07b9b94">HttpRequest 對象包含有關(guān)當前請求的信息,包括 Cookie 和瀏覽器信息。HttpResponse object contains the response that is sent to the client, including all rendered output and cookies." data-guid="2f065779f085181802f04f3222871d2a">HttpResponse 對象包含發(fā)送到客戶端的響應(yīng),包括所有呈現(xiàn)的輸出和 Cookie。

       

       步驟4:HttpApplication object is assigned to the request" data-guid="6adc23d890f4a0ab707312a6c389b5af">將 HttpApplication 對象分配給請求HttpApplication object is assigned to the request" data-guid="6adc23d890f4a0ab707312a6c389b5af">HttpApplication class." data-guid="473f0898adf6e87cd39f4611f88a7108">初始化所有核心應(yīng)用程序?qū)ο笾?,將通過創(chuàng)建 HttpApplication 類的實例啟動應(yīng)用程序。HttpApplication class and uses the derived class to represent the application." data-guid="303b1a8fce57c312b0dfa728c7b3d00d">如果應(yīng)用程序具有 Global.asax 文件,則 ASP.NET 會創(chuàng)建 Global.asax 類(從 HttpApplication 類派生)的一個實例,并使用該派生類表示應(yīng)用程序。

       HttpApplication is created." data-guid="2f259bce31f998868df3b83ad01b7778">第一次在應(yīng)用程序中請求 ASP.NET 頁或進程時,將創(chuàng)建 HttpApplication 的一個新實例。HttpApplication instances might be reused for multiple requests." data-guid="abb9d7fdf05828fda39d6417a1c2c1d9">不過,為了盡可能提高性能,可對多個請求重復使用 HttpApplication 實例。

       

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

       

       

       

       

       

       

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

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

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

       

      如何處理請求。mhpm有4個重要的步驟:

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

       

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

       

      步驟3(P: ASP.NET page):一旦httphandler執(zhí)行,asp.net頁面對象被創(chuàng)建。而asp.net頁面對象被創(chuàng)建,Page對象的事件將被調(diào)用,這可以幫助我們頁面中定制自定義邏輯的處理。有6個重要的事件,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):一旦頁面對象是執(zhí)行和從內(nèi)存中卸載,httpmodule提供網(wǎng)頁執(zhí)行事件可以用來注入自定義處理邏輯。有4個重要的后處理事件postrequesthandlerexecute,releaserequeststate,updaterequestcacheendrequest

       

       

       事件詳細介紹:

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

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

      Page Load ASP.NET控件完成加載,你寫的UI操作邏輯或任何其他邏輯在這里。
      Page Validate 如果你有你的頁面上有驗證邏輯,這里檢查是否符合驗證。
        Render 將頁面最終的輸出發(fā)送到瀏覽器,如果你想做出一些最終的HTML的變化,這是輸出到瀏覽器,你可以在這里輸入你的HTML的邏輯。
      Page Unload 頁面對象從內(nèi)存中卸載。
      HttpModule PostRequestHandlerExecute 在 ASP.NET 事件處理程序(例如,某頁或某個 XML Web service)執(zhí)行完畢時發(fā)生。
      HttpModule ReleaserequestState 在 ASP.NET 執(zhí)行完所有請求事件處理程序后發(fā)生。該事件將使狀態(tài)模塊保存當前狀態(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)用程序以該請求結(jié)束,并發(fā)出 ASP.NET 信號以存儲該請求狀態(tài)。
      HttpModule UpdateRequestCache Before you end, if you want to update your cache.結(jié)束之前,如果您想更新您的高速緩存。
      HttpModule EndRequest 當 ASP.NET 執(zhí)行完事件處理程序以使緩存模塊存儲將用于從緩存為后續(xù)請求提供服務(wù)的響應(yīng)時發(fā)生。

       

      演示代碼:

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

       

      復制代碼
       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     }
      復制代碼

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

      復制代碼
       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中配置:

      HttpModules配置

      頁面第一次加載效果:

      點擊button按鈕后:

      HttpModule

      HttpModule通過在某些事件中注冊,把自己插入ASP.NET請求處理管道。當這些事件發(fā)生的時候,ASP.NET調(diào)用對相應(yīng)的HTTP模塊,這樣該模塊就能處理請求了。

      常用操作:

      1、向每個頁面動態(tài)添加一些備注或說明性的文字:
      2、判斷用戶登錄

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

       

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

      為了驗證請求是否進入HttpHandler ,我們可以在WebConfig中 加入:

      <add verb="*" path="*.aspx" type="WebDemo1.Common.clsHttpHandler,WebDemo1"/>

      調(diào)試程序看看。

       

      綜上我們可以總計出一張圖:

       

       

      ASP.NET頁面事件

           在上面的部分,我們已經(jīng)看到了一個ASP.NET頁面完整的請求事件。 最重要的部分之一,是ASP.NET頁面,我們沒有詳細討論相同。我們詳細討論下ASP.NET的頁面事件。任何ASP.NET頁面中有2個部分 如圖:

      注意 :大部分的開發(fā)者直接使用page_load方法的一切,這不是一個好的做法。例如 填充控件,設(shè)置視圖狀態(tài),應(yīng)用主題等,這些都是發(fā)生在頁面上的負荷。因此,我們可以在適當?shù)氖录砑舆m當?shù)倪壿嫞@將真正使你的代碼整潔 符合邏輯。

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

      OnInit 方法執(zhí)行創(chuàng)建 Page 實例所需的初始化和設(shè)置步驟。在頁生命周期的此階段中,頁中聲明的服務(wù)器控件都已初始化為默認狀態(tài);但每個控件的視圖狀態(tài)尚未填充。Page_Init phase, regardless of whether the other controls are child or parent controls." data-guid="38a63102805c00854156e82b6502cc68">在 Page_Init 階段中,頁中的控件不能訪問該頁的其他服務(wù)器控件,無論其他控件是子控件還是父控件。不一定會創(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 如果你的頁面有驗證,或者你想為您的網(wǎng)頁執(zhí)行驗證。
      6 Event Yes Yes Yes 如果這是通過點擊一個按鈕或下拉的變化,那么相對應(yīng)的事件將被執(zhí)行。
      7  Pre-render Yes Yes Yes 如果你想在這些控件視圖狀態(tài)最后保存之前修改UI對象的結(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

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

       

       

       

       

       

       

       

       

       

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多