ASP.Net中自訂Http處理及應用之HttpModule篇

來源:互聯網
上載者:User

HttpHandler實現了類似於ISAPI Extention的功能,他處理請求(Request)的資訊和發送響應(Response)。HttpHandler功能的實現通過實現IHttpHandler介面來達到。而HttpModule實現了類似於ISAPI Filter的功能。

 

HttpModule的實現

HttpModules實現了類似於ISAPI Filter的功能,在開發上,通常需要經過以下步驟:

1.編寫一個類,實現IhttpModule介面

2.實現Init 方法,並且註冊需要的方法

3.實現註冊的方法

4.實現Dispose方法,如果需要手工為類做一些清除工作,可以添加Dispose方法的實現,但這不是必需的,通常可以不為Dispose方法添加任何代碼。

5.在Web.config檔案中,註冊您編寫的類

下面是一個HttpModules的樣本,在這個樣本中,只是簡單的註冊了HttpApplication 的BeginRequest 和 EndRequest事件,並且通過這些事件的實現方法,將相關的資訊列印出來。

例1:
using System;
using System.Web;
namespace MyModule
{
 public class MyModule : IHttpModule
 {
  public void Init(HttpApplication application)
  {
   application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));
   application.EndRequest += (new
EventHandler(this.Application_EndRequest));
  }
      private void Application_BeginRequest(Object source, EventArgs e)
  {
   HttpApplication Application = (HttpApplication)source;
            HttpResponse Response=Application.Context.Response;
   Response.Write("<h1>Beginning of Request</h1><hr>");
  }
      private void Application_EndRequest(Object source, EventArgs e)
  {
   HttpApplication application = (HttpApplication)source;
   HttpResponse Response=Application.Context.Response;
   Response.Write("<h1>End of Request</h1><hr>");
  }
  public void Dispose()
  {
  }
 }
}

程式的開始引用了如下名稱空間:

using System;
using System.Web;

因為HttpApplication、HttpContext、HttpResponse等類在System.Web中定義,因此,System.Web名稱空間是必須引用的。

MyModule類實現了IhttpModule介面。在Init方法中,指明了實現BeginRequest 和EndRequest 事件的方法。在這兩個方法中,只是簡單的分別列印了一些資訊。

下面,在Web.config檔案中註冊這個類,就可以使用這個HttpModule了,註冊的方法如下:

<configuration>
    <system.web>
        <httpModules>
            <add name=" MyModule " type=" MyModule, MyModule" />
        </httpModules>
    </system.web>
</configuration>

現在來看一下效果。編寫一個Aspx頁面test.aspx,內容如下:

<%
Response.Write("<h1>This is the Page</h1><hr>");
%>

運行以後的介面:

 

 

 

深入研究HttpModule

HttpModule通過對HttpApplication對象的一系列事件的處理來對HTTP處理管道施加影響,這些事件在HttpModule的Init方法中進行註冊,包括:

BeginRequest
AuthenticateRequest
AuthorizeRequest
ResolveRequestCache
AcquireRequestState
PreRequestHandlerExecute
PostRequestHandlerExecute
ReleaseRequestState
UpdateRequestCache
EndRequest

其中部分事件同Global.asax中的事件相對應,對應關係如下:

 

HttpModule Global.asax
BeginRequest Application_BeginRequest
AuthenticateRequest Application_AuthenticateRequest
EndRequest Application_EndRequest

 

在例1中,處理了BeginRequest和EndRequest事件,其他事件的處理方式基本上類似。

同HttpHandler對應來看,這些事件,有些在HttpHandler之前發生,有些在HttpHandler處理完後發生。瞭解事件發生的順序非常重要,因為,伺服器端的對象在不同的時間段有著不同的表現。例子之一是Session的使用。不是所有的事件中都能對Session進行處理,而只能在有限的幾個事件中進行處理。詳細的過程可以參考下面的HTTP Request處理生命週期圖。

 

 

 

使用HttpModule實現許可權系統

我們在開發應用系統的時候,應用系統的許可權控制是非常重要的一個部分。在ASP中,要實現許可權的控制是比較麻煩的事情,因為我們必須在每個需要控制許可權的ASP頁面中添加許可權控制碼,從而控制客戶對頁面的訪問。這樣帶來的問題,除了編寫大量重複代?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.