淺談IHttpHandler

在Web應用開發或接口開發時,處理請求接口IHttpHandler隨處可見,那麼咱們此次來簡單聊一下這個接口。設計模式

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

一、IHttpHandler服務器

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

二、IHttpModulespa

一般用來處理因此請求共同須要的操做,好比對因此請求頁面進行某些相同的檢查功能。設計

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

請求到達以後,實現通過HttpModule處理以後再調用HttpHandler的ProcessRequest()方法進行具體相應的。所以,也不難理解爲何說在HttpModule中作一些對全部請求通用的檢查操做,而將特定類請求的處理放在HttpHandler類中。對象

 

1、IHttpHandler

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

IHttpHandler接口只有兩個成員:接口

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

一、IsReusable:標識該HttpHandler對象可否被其餘實例使用,通常咱們將其置爲True。

二、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文件中的幾個屬性作一下說明:

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

二、verb:表示請求方法,如Get/Post,使用*則表示因此匹配全部。

三、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);
}

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

二、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的類名約定一下對應關係便可。具體實現方式不在說明。

相關文章
相關標籤/搜索