现在的位置: 首页 > 综合 > 正文

转:ASP.NET 2.0利用Httphandler实现URL重写(伪URL及伪静态)

2011年04月24日 ⁄ 综合 ⁄ 共 3621字 ⁄ 字号 评论关闭
我们有时候会见到这样的地址:“http://www.huoho.com/show-12-34.html”,你或许认为在站点服务器根目录“/”下存在名为“show-12-34.html”的文件,其实实际它可能是不存在的,而可能你看到的内容是 “/aspx/show.aspx?type=12&id=34”的内容,为什么要这样做呢?原因有多个方面:首先是增强URL的友好性,记 “show-12-34.html”总比“/aspx/show.aspx?type=12&id=34”好记吧?其次就是方便搜索引擎收录,很多搜索引擎更看好静态HTML页,比如:百度;其次就是出于安全性的考虑,因为这样隐藏了参数“type”、“id”。是不是很有意思呢?

其实这是利用URL重写实现的,下面我就说一下在ASP.NET2.0下我所知道的最简单的实现方法:通过实现接口“IHttpHandler”来接管HTTP请求,Follow me!

1.在资源管理方案中添加一个类,类的代码如下:

  

引用内容 引用内容
//类URLRewriter程序清单:
           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;
                /// <summary>
                /// UrlRewriter URL重写类
                   /// Author:yoyo
                /// blog:http://yangmingsheng.cn/
                /// </summary>
                public class UrlRewriter : IHttpHandler //实现“IHttpHandler”接口
                {
                   public UrlRewriter()
                {
                //
                // TODO: 在此处添加构造函数逻辑
                   //
                }
                public void ProcessRequest(HttpContext Context)
                {
                try
                {
                //取得原始URL屏蔽掉参数
                string Url = Context.Request.RawUrl;
                //建立正则表达式
                     System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex
                     (@"/show-(\d+)-(\d+)\..+",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                //用正则表达式进行匹配
                System.Text.RegularExpressions.Match m =
                              Reg.Match(Url,Url.LastIndexOf("/"));//从最后一个“/”开始匹配
                if (m.Success)//匹配成功
                {
                String RealPath =
                             @"~/aspx/show.aspx?type=" + m.Groups[1] + "&id=" + m.Groups[2];
                //Context.Response.Write(RealPath);
                //Context.RewritePath(RealPath);//(RewritePath 用在无 Cookie 会话状态中。)
                Context.Server.Execute(RealPath);
                }
                else
                               {
                Context.Response.Redirect(Context.Request.Url.ToString());
                }
                }
                catch
                {
                Context.Response.Redirect(Context.Request.Url.ToString());
                }
                }
                /// <summary>
                /// 实现“IHttpHandler”接口所必须的成员
                /// </summary>
                /// <value></value>
                /// Author:yoyo
                /// blog:http://yangmingsheng.cn/
                public bool IsReusable
                {
                get { return false; }
                }
                }

          

2.在web.config文件还要添加一下设置项

在<system.web>节点下添加如下代码:
          

程序代码 程序代码
<httpHandlers>
                <add verb="*" path="*/show-?*-?*.aspx" type="UrlRewriter" />
                </httpHandlers>

                

解释一下:

verb是指允许的动作“GET”、“POST”、“PUT”中的一种或几种,星号“*”表示全部允许;

path是指匹配路径,支持简单的通配符;

type是指绑定的类名以及包括命名空间(如果有的话);

对了,首先你要建立一个WEB窗体“show.aspx”在目录“aspx”下,因为这个文件就是实际接受请求并显示相关内容的页面。

OK!,编译,打开网站输入地址http://localhost/show-12-34.aspx 访问一下,检查看是不是显示的“/aspx/show.aspx?type=12&id=34”的内容呢?!

上面我是设置了匹配ASPX文件,因为IIS里.HTML扩展名默认是不归ASP.NET接管的,如果要接管HTML请求,
请将IIS的扩展名.HTML映射到“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll”,
然后将上面的aspx改成html:

            <httpHandlers>
                <add verb="*" path="*/show-?*-?*.html" type="UrlRewriter" />
                </httpHandlers>
                
            

现在打开网站输入地址http://localhost/show-12-34.html 访问一下~!

抱歉!评论已关闭.