概述html
HttpHandler是一個HTTP請求的真正處理中心,也正是在這個HttpHandler容器中,ASP.NET Framework才真正地對客戶端請求的服務器頁面作出編譯和執行,並將處理事後的信息附加在HTTP請求信息流中再次返回到HttpModule中。服務器
IHttpHandler是什麼測試
IHttpHandler定義了若是要實現一個HTTP請求的處理所必需實現的一些系統約定。HttpHandler與HttpModule不一樣,一旦定義了本身的HttpHandler類,那麼它對系統的HttpHandler的關係將是「覆蓋」關係。ui
IHttpHandler如何處理HTTP請求url
當一個HTTP請求經同HttpModule容器傳遞到HttpHandler容器中時,ASP.NET Framework會調用HttpHandler的ProcessRequest成員方法來對這個HTTP請求進行真正的處理。以一個ASPX頁面爲例,正是在這裏一個ASPX頁面才被系統處理解析,並將處理完成的結果繼續經由HttpModule傳遞下去,直至到達客戶端。spa
對於ASPX頁面,ASP.NET Framework在默認狀況下是交給System.Web.UI.PageHandlerFactory這個HttpHandlerFactory來處理的。所謂一個HttpHandlerFactory,所謂一個HttpHandlerFactory,是指當一個HTTP請求到達這個HttpHandler Factory時,HttpHandlerFactory會提供出一個HttpHandler容器,交由這個HttpHandler容器來處理這個HTTP請求。code
一個HTTP請求都是最終交給一個HttpHandler容器中的ProcessRequest方法來處理的。htm
圖1:ProcessRequest方法對象
一個簡單的HttpHandler容器blog
經過實現IHttpHandler接口能夠建立自定義HTTP處理程序,該接口只包含兩個方法。經過調用IsReusable,IHttpHandlerFactory能夠查詢處理程序以肯定是否能夠使用同一實例爲多個請求提供服務。ProcessRequest方法將HttpContext實例用做參數,這使它可以訪問Request和Response內部對象。在一個HttpHandler容器中若是須要訪問Session,必須實現IRequiresSessionState接口,這只是一個標記接口,沒有任何方法。
示例1:
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.SessionState; namespace MyHandler { /// <summary> /// 目的:實現一個簡單的自定義HttpHandler容器 /// 做者:文野 /// 聯繫:stwyhm@cnblogs.com /// </summary> public class MyFirstHandler : IHttpHandler,IRequiresSessionState { #region IHttpHandler 成員 public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Write("<h1><b>Hello HttpHandler</b></h1>"); context.Session["Test"] = "測試HttpHandler容器中調用Session"; context.Response.Write(context.Session["Test"]); } #endregion } }
在Web.config中加入以下配置:
<httpHandlers>
<add verb="*" path="*" type="MyHandler.MyFirstHandler, MyHandler"/>
</httpHandlers>
IHttpHandler工廠
ASP.NET Framework實際不直接將相關的頁面資源HTTP請求定位到一個其內部默認的IHttpHandler容器之上,而定位到了其內部默認的IHttpHandler工廠上。IHttpHandler工廠的做用是對IHttpHandler容器進行調度和管理。
IHttpHandlerFactory接口包含兩個方法。GetHandler返回實現IHttpHandler接口的類的實例,ReleaseHandler使工廠能夠重用現有的處理程序實例。
示例2:
using System; using System.Collections.Generic; using System.Text; using System.Web; namespace MyHandler { public class MyHandlerFactory : IHttpHandlerFactory { #region IHttpHandlerFactory 成員 public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { string fname = url.Substring(url.IndexOf('/') + 1); while (fname.IndexOf('/') != -1) fname = fname.Substring(fname.IndexOf('/') + 1); string cname = fname.Substring(0, fname.IndexOf('.')); string className = "MyHandler." + cname; object h = null; try { // 採用動態反射機制建立相應的IHttpHandler實現類。 h = Activator.CreateInstance(Type.GetType(className)); } catch (Exception e) { throw new HttpException("工廠不能爲類型"+cname+"建立實例。",e); } return (IHttpHandler)h; } public void ReleaseHandler(IHttpHandler handler) { } #endregion } public class Handler1 : IHttpHandler { #region IHttpHandler 成員 public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Write("<html><body><h1>來自Handler1的信息。</h1></body></html>"); } #endregion } public class Handler2 : IHttpHandler { #region IHttpHandler 成員 public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Write("<html><body><h1>來自Handler2的信息。</h1></body></html>"); } #endregion } }
參考資料
《ASP.NET深刻解析》