ASP.NET——HttpModule

來源:互聯網
上載者:User
ASP.NET模組——HttpModule[轉]

HttpModule是如何工作的

當一個HTTP請求到達HttpModule時,整個ASP.NET Framework系統還並沒有對這個HTTP請求做任何處理,也就是說此時對於HTTP請求來講,HttpModule是一個HTTP請求的“必經之路”,所以可以在這個HTTP請求傳遞到真正的請求處理中心(HttpHandler)之前附加一些需要的資訊在這個HTTP請求資訊之上,或者針對截獲的這個HTTP請求資訊作一些額外的工作,或者在某些情況下乾脆終止滿足一些條件的HTTP請求,從而可以起到一個Filter過濾器的作用。

樣本1:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace MyHttpModule
{
    /// <summary>
    /// 說明:用來實現自己的HttpModule類。
    /// </summary>
    public class MyFirstHttpModule : IHttpModule
    {
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;

            HttpContext context = application.Context;
            HttpRequest request = application.Request;
            HttpResponse response = application.Response;

            response.Write("我來自自訂HttpModule中的BeginRequest<br />");
        }

        private void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;

            HttpContext context = application.Context;
            HttpRequest request = application.Request;
            HttpResponse response = application.Response;

            response.Write("我來自自訂HttpModule中的EndRequest<br />");
        }

        #region IHttpModule 成員

        public void Dispose()
        {
        }

 

        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(Application_BeginRequest);
            application.EndRequest += new EventHandler(Application_EndRequest);
        }

        #endregion
    }
}

 

在Web.config進行如下配置

<add name="MyFirstHttpModule" type="MyHttpModule.MyFirstHttpModule,MyHttpModule"/>

深入瞭解HttpModule

一個HTTP請求在HttpModule容器的傳遞過程中,會在某一時刻(ResolveRequestCache事件)將這個HTTP請求傳遞給HttpHandler容器。在這個事件之後,HttpModule容器會建立一個HttpHandler的入口執行個體,但是此時並沒有將HTTP請求控制權交出,而是繼續觸發AcquireRequestState事件以及PreRequestHandlerExcute事件。在PreRequestHandlerExcute事件之後,HttpModule視窗就會將控制權暫時交給HttpHandler容器,以便進行真正的HTTP請求處理工作。

而在HttpHandler容器內部會執行ProcessRequest方法來處理HTTP請求。在容器HttpHandler處理完畢整個HTTP請求之後,會將控制權交還給HttpModule,HttpModule則會繼續對處理完畢的HTTP請求資訊流進行層層的轉交動作,直到返回到用戶端為止。

圖1:HttpModule生命週期

樣本2:驗證HttpModule生命週期

 

 using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace MyHttpModule
{
    public class ValidaterHttpModule : IHttpModule
    {
        #region IHttpModule 成員

        public void Dispose()
        { }

        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(application_BeginRequest);

            application.EndRequest += new EventHandler(application_EndRequest);

            application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);

            application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);

            application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);

            application.AcquireRequestState += new EventHandler(application_AcquireRequestState);

            application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);

            application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);

            application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);

            application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);

            application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);
        }

        void application_PreSendRequestContent(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_PreSendRequestContent<br/>");
        }

        void application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_PreSendRequestHeaders<br/>");
        }

        void application_ResolveRequestCache(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_ResolveRequestCache<br/>");
        }

        void application_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_AuthorizeRequest<br/>");
        }

        void application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_AuthenticateRequest<br/>");
        }

        void application_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_AcquireRequestState<br/>");
        }

        void application_ReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_ReleaseRequestState<br/>");
        }

        void application_PostRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");
        }

        void application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");
        }

        void application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_EndRequest<br/>");
        }

        void application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.Context.Response.Write("application_BeginRequest<br/>");
        }

        #endregion
    }
}

 

多個自訂的Http Module的運作

從運行結果可以看到,在web.config檔案中引入自訂HttpModule的順序就決定了多個自訂HttpModule在處理一個HTTP請求的接管順序。註:系統預設那幾個HttpModule是最先衩ASP.NET Framework所載入上去的。

樣本3:(代碼類同樣本2)

在HttpModule中終止此次的HTTP請求

可以利用HttpModule通過調用HttpApplication.CompleteRequest()方法實現當滿足某一個條件時終止此次的HTTP請求。

需要注意的是,即使調用了HttpApplication.CompleteRequest()方法終止了一個HTTP請求,ASP.NET Framework仍然會觸發HttpApplication後面的這3個事件:EndRequest事件、PreSendRequestHeaders事件、PreSendRequestContent事件。

如果存在多個自訂的HttpModule的話,當Module1終止了一個HTTP請求,這個HTTP請求將不會再觸發Module2中相應的事件了,但Module2的最後三個事件仍會被觸發。

樣本4:

 

 using System;

using System.Collections.Generic;

using System.Text;
using System.Web;

namespace MyHttpModule
{
    public class CompleteRequestHttpModule : IHttpModule
    {
        #region IHttpModule 成員

        public void Dispose()
        { }

        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(Application_BeginRequest);
        }

        void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            application.CompleteRequest();
            application.Context.Response.Write("請求被終止。");
        }

        #endregion
    }
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.