我们有时候会见到这样的地址:“​http://www.huoho.com/show-12-34.html" target="_blank">​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

          ​

                /// </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

       ​

                public bool IsReusable

                {

                get { return false; }

                }

                }




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


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

           http%3A%2F%2Fwww.iileffel.cn%2Fimages%2Fcode.gif%22%2C%22status%22%3A%22error%22%2C%22alt%22%3A%22%E7%A8%8B%E5%BA%8F%E4%BB%A3%E7%A0%81%22%2C%22percent%22%3A0%2C%22size%22%3A%7B%22width%22%3A0%2C%22height%22%3A0%7D%2C%22id%22%3A%22OuMT3%22%7D" data-card-type="inline" data-card-key="image" data-card-loading="true" data-card-editable="false">上传图片失败! 程序代码<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" target="_blank">​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>