天天看點

IHttphandler之Url重寫

IHttphandler是什麼,表面上看他是一個接口,實際上他可以把用戶端送出的請求進行處理後傳回給用戶端。

IHttphandler接口包括IsReusable屬性和ProcessRequest方法,IsReusable屬性是指是否可以重用,ProcessRequest方法用來處理用戶端的請求。

群裡有人提出了Url重寫的需求,閑來無事便幫忙實作了下。那位兄弟的需求如下:

輸入如下格式

http://localhost:2868/cgi/redirectform?uid=pxl&pwd=12346

即可看到URL重寫實際顯示的頁面為

handler下的RedirectForm.aspx

首先來看下web.config裡的配置資訊:

<httpHandlers>

      <add verb="*" path="cgi/*" type="MyHttpHandler.UrlChangeHandler,MyHttpHandler"/>    </httpHandlers>

1)verb代表用戶端請求類型,“*”代表任何請求類型,當然也可以是“GET,HEAD”,“POST,GET”等。

2)path代表用戶端請求路徑,這個很有用途滴,伺服器控件的響應可以利用(後續的驗證碼伺服器控件文章中有較長的描述,敬請觀看),path設定為“cgi/*”其意義是如果用戶端發出的路徑請求,其中“*”代表任意字元串,比如:cgi/test,cgi/123456,cgi/redirectform?uid=pxl&pwd=123456等。

3)type顧名思義就是類了,格式為“[命名空間].[處理類名稱],[命名空間]”,其中的處理類名稱即為實作了IHttphandler接口的類的名詞。

4)總的來說,此配置的意義為,用戶端發出的verb類型為“*”的請求(任意類型請求) ,請求的路徑格式為“cgi/*”,那麼在伺服器端會由UrlChangeHandler類進行處理。

Url重寫的類可以寫成Dll,當需要Url重寫時将該Dll引入,并且在web.config進行配置即可實作。

下面我們來實作我們的處理類:

using  System;

using  System.Collections.Generic;

using  System.Text;

using  System.Web;

using  System.Web.UI;

namespace  MyHttpHandler

{

     public   class  UrlChangeHandler : IHttpHandler

    {

         #region  IHttpHandler Members

         public   bool  IsReusable

        {

             get  {  return   true ; }

        }

         public   void  ProcessRequest(HttpContext context)

        {

             if  (context.Request.Path.IndexOf( " aspx " )  !=   - 1 )

            {

                 return ;

            }

            StringBuilder sbObj  =   new  StringBuilder();

            sbObj.Append(context.Request.Path);

            sbObj.Append( " .aspx? " );

            sbObj.Append(context.Request.QueryString.ToString());

             string  url  =  sbObj.ToString().Replace( " cgi " ,  " handler " );

            context.Server.Transfer(url,  true );

        }

         #endregion

    }

}

ProcessRequest方法有一個參數,HttpContext上下文,包含Http請求資訊。

if (context.Request.Path.IndexOf("aspx") != -1)

{

       return;

}

這是對請求的路徑進行判斷如果是正常的請求頁面不進行任何處理,比如請求的路徑為“http://localhost:2868/handler/RedirectForm.aspx” 。如果請求的路徑符合“cgi/*”則進行處理:

StringBuilder sbObj = new StringBuilder();

sbObj.Append(context.Request.Path);

sbObj.Append(".aspx?");

sbObj.Append(context.Request.QueryString.ToString());

string url = sbObj.ToString().Replace("cgi", "handler");

context.Server.Transfer(url, true);

用StringBuilder類對請求的路徑進行重寫,假設現在輸入的路徑為“http://localhost:2868/cgi/redirectform?uid=pxl&pwd=123456”,處理過程如下:

1)首先取得“context.Request.Path” 即“http://localhost:2868/cgi/redirectform”。

2)然後附加“.aspx”即“http://localhost:2868/cgi/redirectform.aspx”。

3)然後将傳遞的參數附加即“http://localhost:2868/cgi/redirectform.aspx?uid=pxl&pwd=123456”。

4)然後将路徑中的“cgi”替換為“handler”即“http://localhost:2868/handler/redirectform?uid=pxl&pwd=123456”。

5)最後使用上下文的Server.Transfer在不改變浏覽器中路徑“http://localhost:2868/cgi/redirectform?uid=pxl&pwd=123456” 的情況下,完成對Url重寫,實際轉向為“http://localhost:2868/handler/redirectform?uid=pxl&pwd=123456”。

好了簡單的Url重寫已經完成,大家去試試吧。

轉載于:https://www.cnblogs.com/pangxiaoliang/archive/2009/06/28/1512646.html

ui