步驟如下:
首先編寫自定義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這個方法中自己編寫處理事件,我們編寫的代碼如下:

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);

}

}
注意在上面的代碼中采用了正規表達式來進行比對,使用正規表達式的好處就是在處理格式化文本時相當靈活。除此之外,我們在處理方法中僅僅對滿足要求的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 >
這樣我們的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>
在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>
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。
如果有興趣還可以對全站進行僞靜态化處理,所謂的僞靜态化就是所有的連結在浏覽器位址欄裡看到的都是.html這樣的形式,實際上執行的仍是動态頁面,不過這就需要對IIS進行配置才行。 轉載于 http://zhoufoxcn.blog.51cto.com/792419/177841
轉載于:https://www.cnblogs.com/alern/archive/2012/11/14/2769203.html