步骤如下:
首先编写自定义IHttpModule实现,这个定义只定义了两个方法Dispose()和Init()。在这里我们可以不用关注Dispose()这个方法,这个方法是用来实现如何最终完成资源的释放的。在Init方法中有一个HttpApplication参数,可以在方法中可以自定义对HttpApplication的事件处理方法。比如这里我们的代码如下:
public
void Init(HttpApplication context)
{
//context.BeginRequest是开始处理HTTP管线请求时发生的事件
context.BeginRequest += new EventHandler(context_BeginRequest);
//context.Error是当处理过程中发生异常时产生的事件
//context.Error += new EventHandler(context_Error);
}
{
//context.BeginRequest是开始处理HTTP管线请求时发生的事件
context.BeginRequest += new EventHandler(context_BeginRequest);
//context.Error是当处理过程中发生异常时产生的事件
//context.Error += new EventHandler(context_Error);
}
然后在context_BeginRequest这个方法中自己编写处理事件,我们编写的代码如下:
void context_BeginRequest(
object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path);
//重写后的URL地址
Regex regex= new Regex( "UserInfo(\\d+).aspx",RegexOptions.Compiled);
Match match=regex.Match(file);
//如果满足URL地址重写的条件
if(match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = "UserInfo.aspx?UserId=" + userId;
//将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行
context.RewritePath(rewritePath);
}
}
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path);
//重写后的URL地址
Regex regex= new Regex( "UserInfo(\\d+).aspx",RegexOptions.Compiled);
Match match=regex.Match(file);
//如果满足URL地址重写的条件
if(match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = "UserInfo.aspx?UserId=" + userId;
//将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行
context.RewritePath(rewritePath);
}
}
注意在上面的代码中采用了正则表达式来进行匹配,使用正则表达式的好处就是在处理格式化文本时相当灵活。除此之外,我们在处理方法中仅仅对满足要求的URL进行重写,对于不满足要求的URL则无需进行重写,所以这样就不会干扰没有重写的URL的正常运行(比如Index.aspx)。
从那段从《ASP.NET夜话》摘出的话中可以看出,仅仅是编写自己的IHttpModule实现还是不够的,我们还需要让处理Web请求的程序直到我们编写的IHttpModule实现的存在,这就需要在web.config中配置。在本实例中只需要在本ASP.NET项目中的web.config节点中增加一个<httpModules></httpModules>节点(如果已存在此节点则可以不用添加),然后在此节点中增加一个配置即可,对于本实例,这个节点最终内容如下:
<
httpModules
>
< add name ="UrlReWriter" type ="UrlReWriter" />
</ httpModules >
< add name ="UrlReWriter" type ="UrlReWriter" />
</ httpModules >
这样我们的URL地址重写就成功了。
下面一个演示显示用户列表的链接的页面设计代码:
<
%@ Page
Language
="C#"
AutoEventWireup
="true"
CodeFile
="Default.aspx.cs"
Inherits
="_Default" %
>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title >无标题页 </title>
</head>
< body >
< form id ="form1" runat ="server" >
< div >
< ol >
< li > < a href ="UserInfo1.aspx" >编号为1的用户信息 </a> </li>
< li > < a href ="UserInfo2.aspx" >编号为2的用户信息 </a> </li>
< li > < a href ="UserInfo3.aspx" >编号为3的用户信息 </a> </li>
< li > < a href ="UserInfo4.aspx" >编号为4的用户信息 </a> </li>
< li > < a href ="UserInfo.aspx?UserId=5" >编号为5的用户信息 </a> </li>
</ol>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title >无标题页 </title>
</head>
< body >
< form id ="form1" runat ="server" >
< div >
< ol >
< li > < a href ="UserInfo1.aspx" >编号为1的用户信息 </a> </li>
< li > < a href ="UserInfo2.aspx" >编号为2的用户信息 </a> </li>
< li > < a href ="UserInfo3.aspx" >编号为3的用户信息 </a> </li>
< li > < a href ="UserInfo4.aspx" >编号为4的用户信息 </a> </li>
< li > < a href ="UserInfo.aspx?UserId=5" >编号为5的用户信息 </a> </li>
</ol>
</div>
</form>
</body>
</html>
在UserInfo.aspx页面的设计代码如下:
<
%@ Page
Language
="C#"
AutoEventWireup
="true"
CodeFile
="UserInfo.aspx.cs"
Inherits
="UserInfo" %
>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title >无标题页 </title>
</head>
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Literal ID ="Literal1" runat ="server" > </asp:Literal>
< br />
< img src ="images/13.jpg" width ="283" height ="485" title ="MM" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title >无标题页 </title>
</head>
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Literal ID ="Literal1" runat ="server" > </asp:Literal>
< br />
< img src ="images/13.jpg" width ="283" height ="485" title ="MM" />
</div>
</form>
</body>
</html>
UserInfo.aspx页面的对应逻辑代码如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class UserInfo : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string queryString = Request.QueryString[ "UserId"];
int userId=0;
if ( int.TryParse(queryString, out userId))
{
Literal1.Text = "这是编号为" + userId.ToString() + "的用户的信息。";
}
else
{
Literal1.Text = "错误的参数";
}
}
}
}
尽管我们进行了URL地址重写并不会页面的最终显示效果,因为最终实际执行的还是UserInfo.aspx?UserId=数字这种形式的URL。
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class UserInfo : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string queryString = Request.QueryString[ "UserId"];
int userId=0;
if ( int.TryParse(queryString, out userId))
{
Literal1.Text = "这是编号为" + userId.ToString() + "的用户的信息。";
}
else
{
Literal1.Text = "错误的参数";
}
}
}
}
如果有兴趣还可以对全站进行伪静态化处理,所谓的伪静态化就是所有的链接在浏览器地址栏里看到的都是.html这样的形式,实际上执行的仍是动态页面,不过这就需要对IIS进行配置才行。