標籤:translate path ted 理解 設計 程式 匹配 語言特性 情況
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 IHttpHandler2 {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 //void21 }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實現方式。
Asp.Net IHttpHandler介紹