Asp.Net IHttpHandler介紹


Asp.Net IHttpHandler介紹

ASP.NET響應Http請求時常用的兩個處理接口是IHttpHandler和IHttpModule。

一般的,IHttpHandler用來處理一類特定的請求,比如對每個*.asp, *.aspx文件的分別處理。而IHttpModule通常用來處理所以請求共同需要的操作,比如對所以請求頁面進行某些相同的檢查功能。

我們先來看一下IIS服務器在相應Http請求時的處理步驟。 ASP.NET中有管線(Pipeline)這個概念,意指每個ASP.NET請求在IIS中會有一系列相應操作串聯起來形成的一條類似線一樣的序列。

ASP.NET管線介紹

我們來看一下管線的處理時序圖:

從圖中看出,請求到達之后,實現經過HttpModule處理之后再調用HttpHandler的ProcessRequest()方法進行具體相應的。因此,也不難理解為什么說在HttpModule中做一些對所有請求通用的檢查操作,而將特定類請求的處理放在HttpHandler類中。

代碼實踐

IHttpHandler

筆者最近在項目中接觸到用IHttpHandler來實現對客戶端接口調用的處理,這里便來簡單探討下基於IHttpHandler的簡單接口設計。

IHttpHandler接口只有兩個成員:  

1 public interface IHttpHandler
2 {
3     bool IsReusable { get; }
4     void ProcessRequest(HttpContext context); 
5 }

IsReusable屬性是標識改HttpHandler對象能否被其他實例使用,一般我們將其置為True。 ProcessRequest()方法則是具體的響應請求方法,我們只要將具體的業務邏輯操作放在這里即可。

首先,新建一個Web工程,添加一個Handler類:

 1 public class RayHandler : IHttpHandler
 2 {
 3     public bool IsReusable
 4     {
 5         get { return true; }
 6     }
 7 
 8     public void ProcessRequest(HttpContext context)
 9     {
10         context.Response.Write("Asp.Net HttpHandler Demo. -- .");
11     }
12 }

RayHandler類實現了IHttpHandler接口的ProcessRequest()函數,這里只是直接輸出一條文本。

然后,我們需要在Web.config文件中添加以下配置:

<handlers>
  <add name="test" path="*.ray" verb="*" type="WebApplication2.RayHandler,WebApplication2"/>
</handlers>

path表示URL匹配,如*.ray這表示該Handler會響應所以以".ray"結尾的URL請求,verb表示請求方法,如Get/Post,使用*則表示所以匹配所有。type指示Handler類的類型,WebApplication2.RayHandler是類名,WebApplication2是指Bin目錄下該該程序集的名稱,如示例中的程序集名稱為WebApplication2.dll,且這里不需要制定后綴名。

啟動站點,輸入以".ray"結尾的URL,可以看到如下結果:

IHttpHandlerFactory概述

有時候我們可能需要處理多種不同的后綴,一個后綴對應一個Handler類,這時我們的Web.config文件看起來就是這樣了:

<handlers>
  <add name="test" path="*.ray" verb="*" type="WebApplication2.RayHandler,WebApplication2"/>
  <add name="test1" path="*.rss" verb="*" type="WebApplication2.RssHandler,WebApplication2"/>
</handlers>

如果我們有很多的HttpHandler實現類,那么我們的Web.config文件配置勢必會顯得很冗長。或者在某些情況下,我們只有當程序運行時才能確定哪個Handler進行響應時,這個時候就需要使用IHttpHandlerFactory了。

IHttpHandlerFactory的定義如下:

public interface IHttpHandlerFactory{
    IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated);
    void ReleaseHandler(IHttpHandler handler);
}

其中:

  • GetHandler(): 返回一個實現了IHttpHandler接口的實例;
  • ReleaseHandler():使得Factory可以重復使用一個已經存在Handler實例。

以上述ray,rss請求為例,實現Factory類:

 1 public class HandlerFactory : IHttpHandlerFactory{
 2     public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated){
 3         IHttpHandler handler = null;
 4         string path = context.Request.PhysicalPath;
 5         switch(Path.GetExtension(path)){
 6             case ".ray":
 7                 handler = new RayHandler();
 8                 break;
 9             case ".rss":
10                 handler = new RssHandler();
11                 break;
12             default:
13                 break;
14         }
15 
16         return handler;
17     } 
18 
19     public void ReleaseHandler(IHttpHandler handler){
20         //void
21     }
22 }

這時,在Web.config中的配置如下:

<handlers>
    <add name="test1" path="*.ray,*.rss" verb="*" type="WebApplication2.FactoryHandler,WebApplication2"/>
</handlers> 

這時就實現了用Factory類來對應不同的具體Handler的功能,簡化了配置。

可擴展的IHttpHandlerFactory

上述的實現方式中,如果程序后續需要增加對新后綴的處理方法,就需要修改GetHandler()中的Switch語句,同樣可能引發錯誤或帶來其他安全隱患。那么,能否實現在后續擴展時,保持HandlerFactory類不變呢?

答案肯定是可以的。 熟悉設計模式的讀者應該明白這里是一個簡單工廠模式,要實現前面的功能我們用叫高級點的設計模式是可以實現的。

而在這里,我們還可以用C#語言的語言特性--反射。 通過C#的反射機制,我們根據URL的后綴來反射獲取對應的Hanlder類型,只要我們將URL的后綴名跟Handler的類名約定一下對應關系即可。

如,我們對GetHandler()重寫如下:

 using System.Reflection;

 public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated){
        IHttpHandler handler = null;
        string path = context.Request.PhysicalPath;
        string className = this.GetType().Namespace + Path.GetExtension(path);
        try{
            Type type = Type.GetType(className);
            if(type == null){
                context.Response.Write(string.Format("找不到"{0}"對應的處理程序",Path.GetExtension(path)));
            }
        
            handler = (IHttpHandler)type.Assembly.CreateInstance(className);
        }catch(Exception ex){
            context.Response.Write(string.Format("請求地址錯誤:"+ ex.Message));
            handler = null;
        }
        
        return handler;
    } 

此時,只需要將方法中的Handler類放在HandlerFactory類的同一命名空間下,且在Web.config中正確配置。如,有RayHandler類,那么在應該添加一個如下的配置才能自動匹配:

<handlers>
    <add name="test1" path="*.RayHandler" verb="*" type="WebApplication2.FactoryHandler,WebApplication2"/>
</handlers> 

 

總結

本篇簡單介紹了ASP.NET中IHttpHandler的用法,在多個Handler請求的處理方面,提供了IHttpHandlerFactory的實現方式,最后,利用C#的反射機制改進了一種可擴展的多請求Handler實現方式。


免責聲明!

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



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