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"