淺談IHttpHandler


在Web應用開發或接口開發時,處理請求接口IHttpHandler隨處可見,那么我們這次來簡單聊一下這個接口。

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

1、IHttpHandler

一般用來處理一類特定的請求,比如對每個*.asp, *.aspx文件的分別處理。

2、IHttpModule

通常用來處理所以請求共同需要的操作,比如對所以請求頁面進行某些相同的檢查功能。

我們先來看一下IIS服務器在相應Http請求時的處理步驟。

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

 

一、IHttpHandler

首先我們來看一下IHttpHandler接口設計。

IHttpHandler接口只有兩個成員:

 public interface IHttpHandler
 {
     bool IsReusable { get; }
     void ProcessRequest(HttpContext context); 
 }

1、IsReusable:標識該HttpHandler對象能否被其他實例使用,一般我們將其置為True。

2、ProcessRequest():具體響應請求方法,我們只要將具體的業務邏輯操作放在這里即可。

實踐:

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

public class RayHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write("Asp.Net HttpHandler Demo. -- .");
    }
}

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

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

對config文件中的幾個屬性做一下說明:

1、path:表示URL匹配,如*.ray這表示該Handler會響應所以以".ray"結尾的URL請求。

2、verb:表示請求方法,如Get/Post,使用*則表示所以匹配所有。

3、type:指示Handler類的類型,上面的config文件中,WebApplication2.RayHandler是類名,WebApplication2是指Bin目錄下該該程序集的名稱(不帶.dll后綴)。

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

 

問題:

有時候我們可能需要處理多種不同的后綴,一個后綴對應一個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文件配置勢必會顯得很冗長。

解決問題:

為了解決以上問題,需要使用IHttpHandlerFactory。一看這個接口的名字,猜測是以工廠模式實現的。首先我們來看一下他的接口構成:

IHttpHandlerFactory

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

1、GetHandler(): 返回一個實現了IHttpHandler接口的實例。

2、ReleaseHandler():使得Factory可以重復使用一個已經存在Handler實例。

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

public class HandlerFactory : IHttpHandlerFactory{
    public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated){
        IHttpHandler handler = null;
        string path = context.Request.PhysicalPath;
        switch(Path.GetExtension(path)){
            case ".ray":
                handler = new RayHandler();
                break;
            case ".rss":
                handler = new RssHandler();
                break;
            default:
                break;
        }

        return handler;
    } 

    public void ReleaseHandler(IHttpHandler handler){
        //void
    }
}

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

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

使用了IHttpHandlerFactory,那么我們的config文件的配置相對就簡化了很多。

問題:

 

如果程序后續需要增加對新后綴的處理方法,就需要修改GetHandler()中的Switch語句,可能引發錯誤或帶來其他安全隱患,這樣做也違反了設計原則中的開放封閉原則。那么,如何才能夠實現在后續擴展時,保持HandlerFactory類不變呢?

解決問題:

 

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

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


 


免責聲明!

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



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