瞭解 IHttpHandler
1 、概述
說明:HttpHandler是一個HTTP請求的真正處理中心。在HttpHandler容器中,ASP.NET Framework才調用HttpHandler的ProcessRequest成員方法來對這個HTTP請求進行真正的處理,真正地對客戶端請求的服務器頁面做出編譯和執行,並將處理過後的信息附加在HTTP請求信息流中再次返回到HttpModule中。
2、舉例
以一個aspx頁面爲例,正是在HttpHandler這裏,一個aspx頁面才被系統處理解析,並將處理完成的結果繼續經由HttpModule傳遞下去,直至到達客戶端。當然,對於aspx頁面,ASP.NET Framework在默認情況下是交給System.Web.UI.PageHandlerFactory這個HttpHandlerFactory來處理的。當一個HTTP請求到達這個HttpHandlerFactory時,HttpHandlerFactory會提供出一個HttpHandler容器,交由這個HttpHandler容器來處理這個HTTP請求。
一個HTTP請求都是最終交給一個HttpHandler容器中的ProcessRequest方法來處理的。
3、HttpHandler
(1)實現HttpHandler,必須繼承自IHttpHandler接口。下面是這個接口的定義:
using System;
namespace System.Web
{
public interface IHttpHandler
{
// 其他Request是否可以使用IHttpHandler
bool IsReusable { get; }
// 處理HttpRequest
void ProcessRequest(HttpContext context);
}
}
(2)自定義HttpHandler
新建一個網站,default.aspx頁面:default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br/>來自Default.aspx頁面<br/>");
}
}
}
新添一個類庫MyHandler,添加一個類如下:
using System;
using System.Web;
using System.Web.SessionState;
namespace WebApplication1
{
public class MyTestHandler : IHttpHandler, IRequiresSessionState
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<h3><b>This is a HttpHandler Test</b></h3>");
context.Session["Test"] = "<h3><span style=\"color:blue;\">在HttpHandler容器中調用Session</span></h3>";
context.Response.Write(context.Session["Test"]);
}
}
}
(3)配置文件
在web.config文件的system.web節點下,添加:
<add verb="*" path="*.aspx" type="WebApplication1.MyTestHandler, WebApplication1" />
(4)注意
<1>、.NET爲asp.net提供了很多系統默認HttpHandler類,用來適應不同類型的HttpRequest。比如aspx,在machine.config中是這樣定義的:
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>
這就說明遇到aspx的Request請求,asp.net會將其交給System.Web.UI.PageHandlerFactory的HttpHandler類來處理。
<2>、如果自己定義了新的HttpHandler,而且在web.config中指定,則系統只會使用這個新的HttpHandler,而不再使用原先默認的或者指定的.
4、HttpHandlerFactory
ASP.NET Framework實際不直接將相關的頁面資源HTTP請求定位到一個其內部默認的IHttpHandler容器之上,而定位到了其內部默認的IHttpHandler工廠上。IHttpHandler工廠的作用是對IHttpHandler容器進行調度和管理,這樣做的優點是大大增強了系統的負荷性,提升了效率。
(1)IHttpHandlerFactory接口
IHttpHandlerFactory接口包含兩個方法:GetHandler方法返回實現IHttpHandler接口的類的實例,ReleaseHandler方法使工廠可以重用現有的處理程序實例。
using System;
using System.Web;
namespace System.Web.UI
{
public class PageHandlerFactory : System.Web.IHttpHandlerFactory2, IHttpHandlerFactory
{
protected internal PageHandlerFactory();
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path);
public virtual void ReleaseHandler(IHttpHandler handler);
}
}
(2) 實現一個簡單的HttpHandler工廠
類庫新添一個文件MyHandlerFactor.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class MyHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
string fileName = url.Substring(url.LastIndexOf("/") + 1);
string objName = fileName.Substring(0, fileName.IndexOf("."));
string className = "WebApplication1." + objName;
object objHandler = null;
try
{
// 採用動態反射機制創建相應的IHttpHandler實現類。
objHandler = Activator.CreateInstance(Type.GetType(className));
context.Response.Write(className);
}
catch (Exception e)
{
throw new HttpException("工廠不能爲類型" + objName + "創建實例。", e);
}
return (IHttpHandler)objHandler;
}
public void ReleaseHandler(IHttpHandler handler)
{
}
}
public class Handler1 : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<br/>這是來自於MyHandlerFactory裏的處理.<br/>");
context.Response.Write("<h3>來自Handler1的信息.</h3>");
}
}
public class Handler2 : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<br/>這是來自於MyHandlerFactory裏的處理.<br/>");
context.Response.Write("<h3>來自Handler2的信息.</h3>");
}
}
}
<add verb="*" path="Handler1.aspx" type="WebApplication1.MyHandlerFactory,WebApplication1"/>
<add verb="*" path="Handler2.aspx" type="WebApplication1.MyHandlerFactory,WebApplication1"/>
到這裏,針對Handler1.aspx和Handler2.aspx兩個頁面的http請求我們就通過HttpHandler工廠處理好了。
5、HttpHandler和HttpModule的區別
主要有兩點:
(1)先後次序.先IHttpModule,後IHttpHandler,IHttpHandler處理結束後再交給IHttpModule;
(2)對請求的處理上:
IHttpModule是屬於大小通喫類型,無論客戶端請求的是什麼文件,都會調用到它;例如aspx,html,rar的請求;
IHttpHandler則屬於挑食類型,只有asp.net註冊過的文件類型(例如aspx,ascx,asmx等等)纔會輪到調用它。