什么是HttpHandler?平時所創建的ASP.NET頁面即System.Web.UI.Page類就是一個HttpHandler,因為它實現了IHttpHandler接口.
HttpHandler就是最終響應Http請求,生成Http響應的處理器.它的實例由ASP.NET運行時創建,并生存在ASP.NET運行時環境中.如把ASP.NET運行時比作處理請求的工廠,HttpHandler就是處理請求的工人.
什么情況下自定義HttpHandler呢?一般來說我們響應給客戶端的都是一個HTML頁面,這種情況下,System.Web.UI.Page這個默認的HttpHandler就完全可以勝任,但有時我們響應給客戶端的不是一個HTML頁面,而是XML數據或者圖片等,這時使用自定義的HttpHandler可能是更好的選擇.
HttpContext 類:封裝有關個別 HTTP 請求的所有 HTTP 特定的信息。
為繼承 IHttpModule 和 IHttpHandler 接口的類提供了對當前 HTTP 請求的 HttpContext 對象的引用。該對象提供對請求的內部 Request、Response 和 Server 屬性的訪問。
這是我補充的HttpContext 類:
1.生存周期:從客戶端用戶點擊并產生了一個向服務器發送請求開始---服務器處理完請求并生成返回到客戶端為止.
注:針對每個不同用戶的請求,服務器都會創建一個新的HttpContext實例直到請求結束,服務器銷毀這個實例.
2.為什么會有HttpContext類呢:在ASP年代,大家都是通過在.asp頁面的代碼中使用Request,Respose,Server等等這些Http特定信息的.但在ASP.NET時代,這中方式已經無法滿足應用,(比如我們要在IHttpModule中處理Request時,我們使用什么方法來獲取呢?于是就產生了HttpContext類,它對Request,Respose,Server等等都進行了封裝,并保證在整個請求周期內都可以隨時隨地的調用.)
3.特殊性:當然HttpContext不僅僅只有這點功能.ASP.NET中它還提供了很多特殊的功能.例如Cache.還有HttpContext.Item,通過它你可以在HttpContext的生存周期內提前存儲一些臨時的數據,方便隨時使用.
HttpHandler是一個HTTP請求的真正處理中心,也正是在這個HttpHandler容器中,ASP.NET Framework才真正地對客戶端請求的服務器頁面做出編譯和執行,并將處理過后的信息附加在HTTP請求信息流中再次返回到HttpModule中。
IHttpHandler是什么
IHttpHandler定義了如果要實現一個HTTP請求的處理所必需實現的一些系統約定。HttpHandler與HttpModule不同,一旦定義了自己的HttpHandler類,那么它對系統的HttpHandler的關系將是“覆蓋”關系。
IHttpHandler如何處理HTTP請求
當一個HTTP請求經同HttpModule容器傳遞到HttpHandler容器中時,ASP.NET Framework會調用HttpHandler的ProcessRequest成員方法來對這個HTTP請求進行真正的處理。以一個ASPX頁面為例,正是在這里一個ASPX頁面才被系統處理解析,并將處理完成的結果繼續經由HttpModule傳遞下去,直至到達客戶端。
對于ASPX頁面,ASP.NET Framework在默認情況下是交給System.Web.UI.PageHandlerFactory這個HttpHandlerFactory來處理的。所謂一個HttpHandlerFactory,所謂一個HttpHandlerFactory,是指當一個HTTP請求到達這個HttpHandler Factory時,HttpHandlerFactory會提供出一個HttpHandler容器,交由這個HttpHandler容器來處理這個HTTP請求。
一個HTTP請求都是最終交給一個HttpHandler容器中的ProcessRequest方法來處理的。
一個簡單的HttpHandler容器
通過實現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容器
/// </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
}
}
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
<add verb="*" path="*.html" type="MyHandler.MyFirstHandler, MyHandler"/>
<add verb="*" path="*.htm" type="ImageHandler, ImageHandler"/>
</httpHandlers>