HttpHandler詳解

概述

HttpHandler是一個HTTP請求的真正處理中心,也正是在這個HttpHandler容器中,ASP.NET Framework才真正地對客戶端請求的服務器頁面做出編譯和執行,並將處理過後的信息附加在HTTP請求信息流中再次返回到HttpModule中。


IHttpHandler是什麼

IHttpHandler定義瞭如果要實現一個HTTP請求的處理所必需實現的一些系統約定。HttpHandler與HttpModule不同,一旦定義了自己的HttpHandler類,那麼它對系統的HttpHandler的關係將是“覆蓋”關係。

IHttpHandler如何處理HTTP請求

當一個HTTP請求經同HttpModule容器傳遞到HttpHandler容器中時,ASP.NET Framework會調用HttpHandler的Proce***equest成員方法來對這個HTTP請求進行真正的處理。以一個ASPX頁面爲例,正是在這裏一個ASPX頁面才被系統處理解析,並將處理完成的結果繼續經由HttpModule傳遞下去,直至到達客戶端。

對於ASPX頁面,ASP.NET Framework在默認情況下是交給System.Web.UI.PageHandlerFactory這個HttpHandlerFactory來處理的。所謂一個HttpHandlerFactory,所謂一個HttpHandlerFactory,是指當一個HTTP請求到達這個HttpHandler Factory時,HttpHandlerFactory會提供出一個HttpHandler容器,交由這個HttpHandler容器來處理這個HTTP請求。

一個HTTP請求都是最終交給一個HttpHandler容器中的Proce***equest方法來處理的。



一個簡單的HttpHandler容器

通過實現IHttpHandler接口可以創建自定義HTTP處理程序,該接口只包含兩個方法。通過調用IsReusable,IHttpHandlerFactory可以查詢處理程序以確定是否可以使用同一實例爲多個請求提供服務。Proce***equest方法將HttpContext實例用作參數,這使它能夠訪問Request和Response內部對象。在一個HttpHandler容器中如果需要訪問Session,必須實現IRequiresSessionState接口,這只是一個標記接口,沒有任何方法。


示例1:

[csharp] view plaincopy

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.Text;  

  4. using System.Web;  

  5. using System.Web.SessionState;  

  6.    

  7. namespace MyHandler  

  8. {  

  9.     /// <summary>  

  10.     /// 目的:實現一個簡單的自定義HttpHandler容器  

  11.     /// 作者:文野  

  12.     /// 聯繫:[email protected]  

  13.     /// </summary>  

  14.     public class MyFirstHandler : IHttpHandler,IRequiresSessionState  

  15.     {  

  16.         #region IHttpHandler 成員  

  17.    

  18.         public bool IsReusable  

  19.         {  

  20.             get { return true; }  

  21.         }  

  22.    

  23.         public void Proce***equest(HttpContext context)  

  24.         {  

  25.             context.Response.Write("<h1><b>Hello HttpHandler</b></h1>");  

  26.             context.Session["Test"] = "測試HttpHandler容器中調用Session";  

  27.             context.Response.Write(context.Session["Test"]);  

  28.         }  

  29.   

  30.         #endregion  

  31.     }  

  32. }  


Web.config中加入如下配置:

[html] view plaincopy

  1. <httpHandlers>  

  2.      <add verb="*" path="*" type="MyHandler.MyFirstHandler, MyHandler"/>  

  3. </httpHandlers>  




IHttpHandler工廠

ASP.NET Framework實際不直接將相關的頁面資源HTTP請求定位到一個其內部默認的IHttpHandler容器之上,而定位到了其內部默認的IHttpHandler工廠上。IHttpHandler工廠的作用是對IHttpHandler容器進行調度和管理。

IHttpHandlerFactory接口包含兩個方法。GetHandler返回實現IHttpHandler接口的類的實例,ReleaseHandler使工廠可以重用現有的處理程序實例。

 

示例2:

[csharp] view plaincopy

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.Text;  

  4. using System.Web;  

  5.    

  6. namespace MyHandler  

  7. {  

  8.     public class MyHandlerFactory : IHttpHandlerFactory  

  9.     {  

  10.         #region IHttpHandlerFactory 成員  

  11.    

  12.         public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)  

  13.         {  

  14.             string fname = url.Substring(url.IndexOf('/') + 1);  

  15.             while (fname.IndexOf('/') != -1)  

  16.                 fname = fname.Substring(fname.IndexOf('/') + 1);  

  17.             string cname = fname.Substring(0, fname.IndexOf('.'));  

  18.             string className = "MyHandler." + cname;  

  19.    

  20.             object h = null;  

  21.    

  22.             try  

  23.             {  

  24.                 // 採用動態反射機制創建相應的IHttpHandler實現類。  

  25.                 h = Activator.CreateInstance(Type.GetType(className));  

  26.             }  

  27.             catch (Exception e)  

  28.             {  

  29.                 throw new HttpException("工廠不能爲類型"+cname+"創建實例。",e);  

  30.             }  

  31.    

  32.             return (IHttpHandler)h;  

  33.         }  

  34.    

  35.         public void ReleaseHandler(IHttpHandler handler)  

  36.         {  

  37.              

  38.         }  

  39.   

  40.         #endregion  

  41.     }  

  42.    

  43.     public class Handler1 : IHttpHandler  

  44.     {  

  45.         #region IHttpHandler 成員  

  46.    

  47.         public bool IsReusable  

  48.         {  

  49.             get { return true; }  

  50.         }  

  51.    

  52.         public void Proce***equest(HttpContext context)  

  53.         {  

  54.             context.Response.Write("<html><body><h1>來自Handler1的信息。</h1></body></html>");  

  55.         }  

  56.   

  57.         #endregion  

  58.     }  

  59.    

  60.     public class Handler2 : IHttpHandler  

  61.     {  

  62.         #region IHttpHandler 成員  

  63.    

  64.         public bool IsReusable  

  65.         {  

  66.             get { return true; }  

  67.         }  

  68.    

  69.         public void Proce***equest(HttpContext context)  

  70.         {  

  71.             context.Response.Write("<html><body><h1>來自Handler2的信息。</h1></body></html>");  

  72.         }  

  73.   

  74.         #endregion  

  75.     }  

  76. }  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章