• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            統(tǒng)計(jì)

            • 隨筆 - 50
            • 文章 - 42
            • 評論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 164888
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            HttpHandler
            什么是HttpHandler?平時所創(chuàng)建的ASP.NET頁面即System.Web.UI.Page類就是一個HttpHandler,因?yàn)樗鼘?shí)現(xiàn)了IHttpHandler接口.
                 HttpHandler就是最終響應(yīng)Http請求,生成Http響應(yīng)的處理器.它的實(shí)例由ASP.NET運(yùn)行時創(chuàng)建,并生存在ASP.NET運(yùn)行時環(huán)境中.如把ASP.NET運(yùn)行時比作處理請求的工廠,HttpHandler就是處理請求的工人.
                 什么情況下自定義HttpHandler呢?一般來說我們響應(yīng)給客戶端的都是一個HTML頁面,這種情況下,System.Web.UI.Page這個默認(rèn)的HttpHandler就完全可以勝任,但有時我們響應(yīng)給客戶端的不是一個HTML頁面,而是XML數(shù)據(jù)或者圖片等,這時使用自定義的HttpHandler可能是更好的選擇.
                 HttpContext 類:封裝有關(guān)個別 HTTP 請求的所有 HTTP 特定的信息。
                為繼承 IHttpModule 和 IHttpHandler 接口的類提供了對當(dāng)前 HTTP 請求的 HttpContext 對象的引用。該對象提供對請求的內(nèi)部 Request、Response 和 Server 屬性的訪問。

                這是我補(bǔ)充的HttpContext 類:
                1.生存周期:從客戶端用戶點(diǎn)擊并產(chǎn)生了一個向服務(wù)器發(fā)送請求開始---服務(wù)器處理完請求并生成返回到客戶端為止.
                   注:針對每個不同用戶的請求,服務(wù)器都會創(chuàng)建一個新的HttpContext實(shí)例直到請求結(jié)束,服務(wù)器銷毀這個實(shí)例.
                2.為什么會有HttpContext類呢:在ASP年代,大家都是通過在.asp頁面的代碼中使用Request,Respose,Server等等這些Http特定信息的.但在ASP.NET時代,這中方式已經(jīng)無法滿足應(yīng)用,(比如我們要在IHttpModule中處理Request時,我們使用什么方法來獲取呢?于是就產(chǎn)生了HttpContext類,它對Request,Respose,Server等等都進(jìn)行了封裝,并保證在整個請求周期內(nèi)都可以隨時隨地的調(diào)用.)
                3.特殊性:當(dāng)然HttpContext不僅僅只有這點(diǎn)功能.ASP.NET中它還提供了很多特殊的功能.例如Cache.還有HttpContext.Item,通過它你可以在HttpContext的生存周期內(nèi)提前存儲一些臨時的數(shù)據(jù),方便隨時使用.

            HttpHandler是一個HTTP請求的真正處理中心,也正是在這個HttpHandler容器中,ASP.NET Framework才真正地對客戶端請求的服務(wù)器頁面做出編譯和執(zhí)行,并將處理過后的信息附加在HTTP請求信息流中再次返回到HttpModule中。
            IHttpHandler是什么
            IHttpHandler定義了如果要實(shí)現(xiàn)一個HTTP請求的處理所必需實(shí)現(xiàn)的一些系統(tǒng)約定。HttpHandler與HttpModule不同,一旦定義了自己的HttpHandler類,那么它對系統(tǒng)的HttpHandler的關(guān)系將是“覆蓋”關(guān)系。
            IHttpHandler如何處理HTTP請求
            當(dāng)一個HTTP請求經(jīng)同HttpModule容器傳遞到HttpHandler容器中時,ASP.NET Framework會調(diào)用HttpHandler的ProcessRequest成員方法來對這個HTTP請求進(jìn)行真正的處理。以一個ASPX頁面為例,正是在這里一個ASPX頁面才被系統(tǒng)處理解析,并將處理完成的結(jié)果繼續(xù)經(jīng)由HttpModule傳遞下去,直至到達(dá)客戶端。
            對于ASPX頁面,ASP.NET Framework在默認(rèn)情況下是交給System.Web.UI.PageHandlerFactory這個HttpHandlerFactory來處理的。所謂一個HttpHandlerFactory,所謂一個HttpHandlerFactory,是指當(dāng)一個HTTP請求到達(dá)這個HttpHandler Factory時,HttpHandlerFactory會提供出一個HttpHandler容器,交由這個HttpHandler容器來處理這個HTTP請求。
            一個HTTP請求都是最終交給一個HttpHandler容器中的ProcessRequest方法來處理的。
            一個簡單的HttpHandler容器
            通過實(shí)現(xiàn)IHttpHandler接口可以創(chuàng)建自定義HTTP處理程序,該接口只包含兩個方法。通過調(diào)用IsReusable,IHttpHandlerFactory可以查詢處理程序以確定是否可以使用同一實(shí)例為多個請求提供服務(wù)。ProcessRequest方法將HttpContext實(shí)例用作參數(shù),這使它能夠訪問Request和Response內(nèi)部對象。在一個HttpHandler容器中如果需要訪問Session,必須實(shí)現(xiàn)IRequiresSessionState接口,這只是一個標(biāo)記接口,沒有任何方法。
            示例1:
            using System;
            using System.Collections.Generic;
            using System.Text;
            using System.Web;
            using System.Web.SessionState;

            namespace MyHandler
            {
                /// <summary>
                /// 目的:實(shí)現(xiàn)一個簡單的自定義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容器中調(diào)用Session";
                        context.Response.Write(context.Session["Test"]);
                    }

                    #endregion
                }
            }
            在Web.config中加入如下配置:
            < httpHandlers >
                 <add verb="*" path="*" type="MyHandler.MyFirstHandler, MyHandler"/>
            </httpHandlers>

            IHttpHandler工廠
            ASP.NET Framework實(shí)際不直接將相關(guān)的頁面資源HTTP請求定位到一個其內(nèi)部默認(rèn)的IHttpHandler容器之上,而定位到了其內(nèi)部默認(rèn)的IHttpHandler工廠上。IHttpHandler工廠的作用是對IHttpHandler容器進(jìn)行調(diào)度和管理。
            IHttpHandlerFactory接口包含兩個方法。GetHandler返回實(shí)現(xiàn)IHttpHandler接口的類的實(shí)例,ReleaseHandler使工廠可以重用現(xiàn)有的處理程序?qū)嵗?
            示例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
                        {
                            // 采用動態(tài)反射機(jī)制創(chuàng)建相應(yīng)的IHttpHandler實(shí)現(xiàn)類。
                            h = Activator.CreateInstance(Type.GetType(className));
                        }
                        catch (Exception e)
                        {
                            throw new HttpException("工廠不能為類型"+cname+"創(chuàng)建實(shí)例。",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>

            posted on 2009-03-05 10:02 pear_li 閱讀(746) 評論(0)  編輯 收藏 引用 所屬分類: asp.net

            精品99久久aaa一级毛片| 最新久久免费视频| 久久久久亚洲AV无码专区体验| 久久精品国产99国产精品亚洲 | 久久久久亚洲av毛片大| 亚洲一区精品伊人久久伊人| 亚洲精品乱码久久久久久久久久久久 | 久久久久亚洲AV无码麻豆| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 久久国产精品99精品国产| 97精品伊人久久久大香线蕉| 色99久久久久高潮综合影院| 久久精品99久久香蕉国产色戒| 久久综合色区| 一本久久久久久久| 中文字幕无码精品亚洲资源网久久| 国产精品一久久香蕉产线看| 人妻中文久久久久| 久久精品国产69国产精品亚洲| 久久人与动人物a级毛片| 国产成人精品久久亚洲| 少妇久久久久久久久久| 四虎国产精品免费久久| 久久精品这里只有精99品| 国产日产久久高清欧美一区| 奇米影视7777久久精品| 久久久久久伊人高潮影院| 亚洲人成无码网站久久99热国产| 久久精品嫩草影院| 91精品国产高清91久久久久久| 99精品久久久久久久婷婷| 亚洲国产成人久久综合碰| 久久人人爽人人爽AV片| 国产99久久九九精品无码| 久久国产精品久久国产精品| 久久久久久亚洲精品成人| 欧美午夜精品久久久久免费视| 精品久久久无码21p发布| 亚洲AV无码久久精品蜜桃| 久久久亚洲AV波多野结衣| 久久人人爽人人爽人人片av高请|