URL重寫就是首先得到一個進入的URL請求,而後把它從新寫成網站能夠處理的另外一個URL的過程。舉個例子來講,若是經過瀏覽器進來的URL是"list.aspx?id=1",那麼它能夠被重寫成"list.1html",這樣的URL,這樣的網址能夠更好的被網站所閱讀。html
1.首先新建一個WebApplication項目和一個類庫(用於作URLRewrite)web
2.在index.aspx頁面中添加一個按鈕用於跳轉到另一個頁面(跳轉的連接爲:list.1html)瀏覽器
前臺代碼:app
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="URLRewriter.index" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 <asp:Button Text="跳轉" runat="server" ID="btnGo" OnClick="btnGo_Click" /> 14 </div> 15 </form> 16 </body> 17 </html>
後臺代碼:(跳轉到list.1html這個連接)函數
1 protected void btnGo_Click(object sender, EventArgs e) 2 { 3 Response.Redirect("list.1html"); 4 }
3.進行URL重寫網站
Module這個類庫中新建一個類:Module.cs,用於URL的重寫,首先咱們要添加System.Web程序集的引用,以後實現IHttpModule接口ui
1 public class Module : IHttpModule 2 { 3 public void Dispose() 4 { } 5 6 /// <summary> 7 /// 初始化 8 /// </summary> 9 /// <param name="context"></param> 10 public void Init(HttpApplication context) 11 { 12 //一個BeginRequest事件 13 //當瀏覽器輸入URL並跳轉的時候,HTTPRequest開始 14 //在請求開始的時候將會引起這個事件 15 context.BeginRequest += context_BeginRequest; 16 } 17 18 /// <summary> 19 /// BeginRequest事件響應函數 20 /// </summary> 21 /// <param name="sender"></param> 22 /// <param name="e"></param> 23 void context_BeginRequest(object sender, EventArgs e) 24 { 25 //將sender轉成HttpApplication對象 26 HttpApplication app = sender as HttpApplication; 27 28 //得到HTTP請求的路徑,在這個例子中將會得到:"/list.1html"這樣一個路徑 29 string requestPath = app.Context.Request.Path; 30 31 //咱們要獲取路徑的文件名,這裏得到的是 "list.1html" 32 string fileName = Path.GetFileName(requestPath); 33 34 //判斷文件名是否以"list."開頭,若是是,咱們將進行URL的重寫 35 if (fileName.StartsWith("list.")) 36 { 37 //獲取文件的拓展名 ".1html" 38 string extention = Path.GetExtension(fileName); 39 40 //進行URL的重寫,咱們須要獲得的是".1html"中的那個"1",而這個"1"的位數是不固定的 41 //有多是"122312321.html" 42 //因此咱們須要獲取"."到"h"之間的那一段數字 43 //1.將"html"替換爲空字符串,獲得的是".1" 44 string target = extention.Replace("html", ""); 45 //2.使用Substring方法取字串,從第一位開始取,一直取到最後一位 46 //target的值就爲"1" 47 target = target.Substring(1); 48 //3.得到target值以後就能夠重寫URL了,在這裏咱們將"list.1html"重寫成"list.aspx?id=1" 49 app.Context.RewritePath("list.aspx?id=" + target); 50 } 51 }
4.重寫完成以後,頁面的請求會跳轉到"list.aspx"頁面spa
後臺代碼,用來接收和輸出傳進來的參數debug
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (Request.QueryString["id"] != null) 4 { 5 string id = Request.QueryString["id"].ToString(); 6 Response.Write(id); 7 } 8 }
5.還有很重要的一步,就是在Web.config中要作相應的配置code
1 <configuration> 2 <system.web> 3 <compilation debug="true" targetFramework="4.5" /> 4 <httpRuntime targetFramework="4.5" /> 5 </system.web> 6 <system.webServer> 7 <modules> 8 <add type="Module.Module,Module" name="MyModule" /> 9 </modules> 10 </system.webServer> 11 </configuration>
其中, <add type="Module.Module,Module" name="MyModule" />節點中,name能夠隨便寫,type中逗號前面是URL重寫那個類的名稱,就是
"命名空間+類名":Module.Module
逗號後面的是程序集的名稱,也就是類庫的名稱:(Module)
完成這些工做以後,在index.aspx中點擊跳轉按鈕,本應該跳轉到"list.1html"頁面,而在咱們的網站結構中是沒有"list.1html"這個頁面的,咱們進行URL重寫了以後,就跳轉到了"list.aspx?id=1"