一點一點學ASP.NET之基礎概念——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的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方法來處理的。

圖1: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容器
    ///作者:文野
    ///聯繫:[email protected]
    ///</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>
     <addverb="*"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
    }
}


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