HttpHandler实现url重定向

一,先在vs.net2005先新建个web site站点,我命名为RewriteTest,然后添加个404.aspx文件.  
二,修改iis.在iis中找到站点RewriteTest,右击选属性,在弹出的对话面板里面选,再点里面的404,选编辑属性,文件类型选url,下面输入:/RewriteTest/404.aspx  好了,点确定到头.在这里为什么我们不在web.config里面用<customErrors>来配置路径呢?因为在web.config里面设置的如果输入的是一个不存在的文件他能够截获,如果输入的是一个不存在的目录他就不能够截获拉,所以只能够在iis里面设置.  
三,我们现在修改web.config文件,让路径404.aspx来让我们的一个实现了IHttpHandler接口的类来处理他,修改如下:<system.web>  
    <httpHandlers>  
      <add verb="*" type="RewriteTest.Http404" path="404.aspx"/>  
    </httpHandlers>  
其中RewriteTest是名字空间,Http404是一个实现了IHttpHandler接口的类, 这样当我们的404.aspx截获到错误的时候会由这个Http404类来处理] 
四,我们添加个类Http404,代码如下,有详细的注释: 
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
namespace RewriteTest 

    /// <summary> 
    /// Summary description for Http404 
    /// </summary> 
    public class Http404:IHttpHandler 
    { 
        public Http404() 
        { 
            // 
            // TODO: Add constructor logic here 
            // 
        } 

        #region IHttpHandler Members 

        public bool IsReusable 
        { 
            get { throw new Exception("The method or operation is not implemented."); } 
        } 

        public void ProcessRequest(HttpContext context) 
        { 
            //throw new Exception("The method or operation is not implemented."); 
            string errorPath = context.Request.RawUrl.ToLower(); 
            context.Response.Write(errorPath); 
        } 

        #endregion 

        #region IHttpHandler Members 

        bool IHttpHandler.IsReusable 
        { 
            get { return false; } 
        } 

        void IHttpHandler.ProcessRequest(HttpContext context) 
        { 
            //首先我们取得请求的原始url,比如我们请求的是http://localhost/RewriteTest/123456 
            //RewriteTest是这个应用程序的名字,返回的将是/rewritetest/404.aspx?404;http://localhost/rewritetest/123456 
            //请大家注意404后面的; 
            string errorString = context.Request.RawUrl.ToLower(); 
            //context.Response.Write(errorString); 
            //再把得到的地址按照;分割下将得到结果是:http://localhost/rewritetest/123456 
            //这个时候我们再想办法得到那个不存在的虚拟目录 
            errorString = errorString.Split(';')[1]; 
            //也可以写成这样:errorString = errorString.Split(new char[]{';'})[1]; 
            //context.Response.Write(errorString); 
            //如果url含有点.我们就判断他带有后缀如.gif.jpg.asp.......我们就输出请求资源不存在的错误信息 
            if (errorString.IndexOf(".") < 0) 
            { 
                //去掉前面的http:// 
                errorString = errorString.Remove(0, 7); 
                //context.Response.Write(errorString); 
                //再得到域名 
                string hostName = context.Request.Url.Host; 
                errorString = errorString.Remove(0, hostName.Length); 
                //context.Response.Write(errorString); 
                //再得到请求的应用程序路径 
                string appPath = context.Request.ApplicationPath; 
                errorString = errorString.Remove(0, appPath.Length); 
                //context.Response.Write(errorString); 
                if (errorString.StartsWith("/")) 
                { 
                    //如果前面有/就去掉/ 
                    errorString = errorString.Remove(0, 1); 
                } 
                if (errorString.EndsWith("/")) 
                { 
                    //如果以/结尾我们也去掉他 
                    errorString = errorString.Remove(errorString.Length - 1, 1); 
                } 
                if (errorString.IndexOf("/") < 0) 
                { 
                    //如果中间含有/我们就不理他 
                    //好了,我们现在实现url重写,就是页面跳转,在这里我很奇怪为什么不能够用 
                    //context.RewritePath来实现,而要用Server.Transfer 
                    context.Server.Transfer("~/page.aspx?name=" + errorString); 
                } 
            } 
            else 
            { 
                context.Response.Write("懒MM.你请求的资源不存在,请点<a href='default.aspx'>这里</a>返回首页!"); 
            } 
        } 

        #endregion 
    } 

五,我们再添加个page.aspx文件,后台就一句代码: protected void Page_Load(object sender, EventArgs e) 
    { 
        Response.Write(Request.QueryString["name"].ToString()); 
    } 

转载自 http://www.myxici.com/board/d149489.htm

转载于:https://www.cnblogs.com/yzxchoice/archive/2006/11/29/576506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值