Asp.Net生命周期系列四


上回我們說的當一個Http請求來到HttpModule這里的時候,Asp.Net內部並未對這個Http請求做出任何的處理,我們可以對這個Http請求添加一些我們需要的信息,以方便我們控制這個Http請求。我們添加控制信息一般情況下是通過添加一些事件來控制的,那么HttpModule內部到底有哪些事件呢,他們的執行順序又是怎樣的呢?

1、HttpModule的事件

   
BeginRequest 指示請求處理開始
AuthenticateRequest 封裝請求身份驗證過程
AuthorizeRequest 封裝檢查是否能利用以前緩存的輸出頁面處理請求的過程
ResolveRequestCache 從緩存中得到相應時候觸發
AcquireRequestState 加載初始化Session時候觸發
PreRequestHandlerExecute 在Http請求進入HttpHandler之前觸發
PostRequestHandlerExecute 在Http請求進入HttpHandler之后觸發
ReleaseRequestState 存儲Session狀態時候觸發
UpdateRequestCache 更新緩存信息時觸發
EndRequest 在Http請求處理完成的時候觸發
PreSendRequestHenaders 在向客戶端發送Header之前觸發
PreSendRequestConternt 在向客戶端發送內容之前觸發

2、驗證HttpModule生命周期
與HttpHandler的交互:
HttpModuleHandler

說明:
a、HttpModule容器會將HttpRequest傳遞到HttpHandler容器,這個時間點是ResolveRequestCache事件
b、HttpModule容器會建立HttpHandler實例作為入口——Session從此生效
c、觸發AcquireRequestState事件以及PreRequestHandlerExecute事件
d、HttpModule容器便將對HttpRequest的控制權轉讓給HttpHandler容器
e、HttpHandler容器處理HttpRequest——使用自身的ProcessRequest方法,處理完后將對其控制權又還給HttpModule容器——之后Session失效。

在系列一種我們說到,當我們的項目經理(HttpApplication)拿到包含有我們整個請求信息的上下文對象Context(內部包含主要的Request對象和Response對象)之后。它會按照一個由HttpRunTime事先根據webconfig文件建立好的HttpModule列表去工作,其實啊,就是按照這樣一個表的順序去把Http請求流【傳遞】一遍罷了。

那么現在我有個問題想問一下大家,你們認為這個Http請求究竟會怎么樣流過這些HttpModule呢?

我的配置文件是這樣寫的:(MyHttpModule1已經注釋掉了哦,看清楚,寫系列三時用到了MyHttpModule1,)

    <httpModules>
      <!--<add name="MyHttpModel"  type="ClassLibrary1.MyHttpModel,ClassLibrary1"/>-->
      <add name="MyHttpModel2" type="ClassLibrary1.MyHttpModule2,ClassLibrary1"/>
      <add name="MyHttpModel3" type="ClassLibrary1.MyHttpModule3,ClassLibrary1"/>
    </httpModules>

我給你們兩種選擇:

第一種:誰在前面誰是老大啊,那自然是先流過MyHttpModule2,過完之后才能輪到MyHttpModule3啊,這就是所謂的先來先服務嗎,誰讓人家比你靠前呢。

第二種:憑什么你MyHttpModule2在前面Http請求就要先流過你,然后才能輪到我MyHttpModule3呢,不行,必須先輪流着來,這樣才公平嗎。

那么究竟是怎么樣的呢,我不想解釋,直接看代碼

下面是兩個自定義的HttpModule,代碼一模一樣,就是換了個名字

namespace ClassLibrary1
{
    public class MyHttpModule2:IHttpModule
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }
        public void Init(HttpApplication context)
        {
           context.BeginRequest+=new EventHandler(context_BeginRequest);
           context.AuthenticateRequest+=new EventHandler(context_AuthenticateRequest);
           context.AuthorizeRequest+=new EventHandler(context_AuthorizeRequest);
           context.ResolveRequestCache+=new EventHandler(context_ResolveRequestCache);
           context.AcquireRequestState+=new EventHandler(context_AcquireRequestState);
           context.PreRequestHandlerExecute+=new EventHandler(context_PreRequestHandlerExecute);
           context.PostRequestHandlerExecute+=new EventHandler(context_PostRequestHandlerExecute);
           context.ReleaseRequestState+=new EventHandler(context_ReleaseRequestState);
           context.UpdateRequestCache+=new EventHandler(context_UpdateRequestCache);
           context.EndRequest+=new EventHandler(context_EndRequest);
           context.PreSendRequestHeaders+=new EventHandler(context_PreSendRequestHeaders);
           context.PreSendRequestContent+=new EventHandler(context_PreSendRequestContent);
        }
        private void context_BeginRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_BeginRequest<br/>");

        }
        private void context_AuthenticateRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_AuthenticateRequest<br/>");

        }
        private void context_AuthorizeRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_AuthorizeRequest<br/>");

        }
        private void context_ResolveRequestCache(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_ResolveRequestCache<br/>");

        }
        private void context_AcquireRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_AcquireRequestState<br/>");

        }
        private void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_PreRequestHandlerExecute<br/>");

        }
        private void context_PostRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_PostRequestHandlerExecute<br/>");

        }
        private void context_UpdateRequestCache(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            application.Context.Response.Write("MyHttpModule2中的context_UpdateRequestCache<br/>");
        }
        private void context_EndRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_EndRequest<br/>");

        }
        private void context_ReleaseRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_ReleaseRequestState<br/>");

        }
        private void context_PreSendRequestHeaders(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_PreSendRequestHeaders<br/>");

        }
        private void context_PreSendRequestContent(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule2中的application_PreSendRequestContent<br/>");

        }
         
View Code
namespace ClassLibrary1
{
    public class MyHttpModule3:IHttpModule
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
            context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
            context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache);
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
            context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
            context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache);
            context.EndRequest += new EventHandler(context_EndRequest);
            context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }
        private void context_BeginRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_BeginRequest<br/>");

        }
        private void context_AuthenticateRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_AuthenticateRequest<br/>");

        }
        private void context_AuthorizeRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_AuthorizeRequest<br/>");

        }
        private void context_ResolveRequestCache(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_ResolveRequestCache<br/>");

        }
        private void context_AcquireRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_AcquireRequestState<br/>");

        }
        private void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_PreRequestHandlerExecute<br/>");

        }
        private void context_PostRequestHandlerExecute(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_PostRequestHandlerExecute<br/>");

        }
        private void context_UpdateRequestCache(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            application.Context.Response.Write("MyHttpModule3中的context_UpdateRequestCache<br/>");
        }
        private void context_EndRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_EndRequest<br/>");

        }
        private void context_ReleaseRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_ReleaseRequestState<br/>");

        }
        private void context_PreSendRequestHeaders(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_PreSendRequestHeaders<br/>");

        }
        private void context_PreSendRequestContent(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("MyHttpModule3中的application_PreSendRequestContent<br/>");

        }
    }
}
View Code

下面是要請求的Default.aspx頁面

namespace ClassLibrary1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("這是Default.aspx頁面請求的內容信息。");
        }
    }
}
View Code

運行起來,我們請求Default.aspx頁面看到下面的結果:

看到結果我想就不用解釋了吧,Asp.Net聲明周期中選擇的是第二種選擇。

3、利用HttpModule實現終止此次HttpRequest請求

如果我們在MyHttpModule2中的context_BeginRequest事件中就終止了此次的Http請求,那么接下來的事件還會繼續往下執行嗎?

還是上面的兩個自定義HttpModule(MyHttpModule2和MyHttpModule3),我把MyHttpModule2中的context_BeginRequest事件改寫如下:

 private void context_BeginRequest(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;
            application.CompleteRequest();
            application.Context.Response.Write("此次Http請求已經終止<br/>");

        }

運行結果如下:

寫到這里,我想現在客戶端發出一個Http請求經過HttpModule內部時到底怎樣執行,執行哪些事件,以及我們能夠利用這些事件做些什么你們應該有一定的了解了,如果你還沒有了解,請給我留言,再往下我們該介紹真正的處理中心HttpHandler這個東東了,如果您喜歡我的文章,請繼續關注。

本系列:

一:Asp.Net生命周期系列一

二:Asp.Net生命周期系列二

三:Asp.Net生命周期系列三

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM