URL 重写是截取传入 Web 请求并自动将请求重定向到其他 URL 的过程。
比如浏览器发来请求hostname/101.aspx ,服务器自动将这个请求中定向为http://hostname/list.aspx?id=101。
url重写的优点在于:
缩短url,隐藏实际路径提高安全性
易于用户记忆和键入。
易于被搜索引擎收录
二 实现url重写的基本方法
下载MS的URLRewriter.dll,放到你的web程序的bin下
下载地址1:http://www.rickel.cn/uploads/DevTools/MSDNURLRewriting.msi
下载地址2:download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
下载完成后,在web.config里设置如下:
Code
1
<?xml version="1.0" encoding="utf-8" ?>
2
<!--overred-->
3
<configuration>
4
<configSections>
5
<section name="RewriterConfig"type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
6
</configSections>
7
<RewriterConfig>
8
<Rules>
9
<RewriterRule>
10
<LookFor>~/d(\d+)\.aspx</LookFor>
11
<SendTo>~/default.aspx?id=$1</SendTo>
12
</RewriterRule>
13
</Rules>
14
</RewriterConfig>
15
<system.web>
16
<httpHandlers>
17
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
18
</httpHandlers>
19
</system.web>
20
</configuration>
21
22
23
24
其中
<
section
name
="RewriterConfig"
type
="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"
/>
用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter .dll文件中
关键的是这两句
<
LookFor
>
~/d(\d+)\.aspx
</
LookFor
>
<
SendTo
>
~/default.aspx?id=$1
</
SendTo
>
<
LookFor
>
~/d(\d+)\.aspx
</
LookFor
>
表示,用户输入的url,d(\d+)\.aspx是 url中文件名匹配的正则表达式(此处为字母d开头,后面跟一个或多个数字,并以.aspx结尾。用户也可根据自己的需要自行设定)。
<
SendTo
>
~/default.aspx?id=$1
</
SendTo
>
表示当服务器接收到符合上面条件的请求后如何重写url。此处表示访问defalutl.aspx并传入参数id,其值$1将用用户请求的文件名中的第一个数字来表示。
例如用户输入 hostname/d11.aspx,服务器会把他重写为http://hostname/default.aspx?id=11。换句话说用户输入http: //hostname/d11.aspx,实际访问的是http://hostname/default.aspx?id=11。这样就起到了隐藏真实文件名,并便于用户记忆的作用。
处理回发
在重写后的url里如果产生回发,例如有一个按钮,又调用了该被重写的aspx,用户浏览器中将会显示该aspx文件实际的地址,也就是http: //hostname/default.aspx?id=11。但从用户的角度考虑,如 果单击按钮时突然看到 URL 更改会使他们感到不安。因此必须解决这个问题。
解决方法有二:
(1)自己定义一个Actionlessform类,在aspx中不再使用系统提供的form 标记
Code
1
namespace ActionlessForm
2

{
3
public class Form : System.Web.UI.HtmlControls.HtmlForm
4
{
5
protected override void RenderAttributes(HtmlTextWriter writer)
6
{
7
writer.WriteAttribute("name", this.Name);
8
base.Attributes.Remove("name");
9
writer.WriteAttribute("method", this.Method);
10
base.Attributes.Remove("method");
11
this.Attributes.Render(writer);
12
base.Attributes.Remove("action");
13
if (base.ID != null)
14
writer.WriteAttribute("id", base.ClientID);
15
}
16
}
17
}
创建此类并对其进行编译之后,要在 ASP.NET Web 应用程序中使用它,应首先将其添加到 Web 应用程序的 References 文件夹中。然后,要使用它来代替 HtmlForm 类,做法是在 ASP.NET 网页的顶部添加以下内容:
<%
@ Register TagPrefix
=
"
skm
"
Namespace
=
"
ActionlessForm
"
Assembly
=
"
ActionlessForm
"
%>
然后,将 <form runat="server">(如果有)替换为:<skm:Form id="Form1" method="post" runat="server">
并将右边的 </form> 标记替换为:</skm:Form>
个人并不推荐该方法
(2)第二种方法就是继承page,这样你不需要在aspx页中改任何东西。
代码:
Code
1
using System;
2
using System.IO;
3
using System.Web;
4
using System.Web.UI;
5
namespace URL
6

{
7
public class OLPage : Page
8
{
9
public OLPage()
10
{}
11
protected override void Render(HtmlTextWriter writer)
12
{
13
if (writer is System.Web.UI.Html32TextWriter)
14
{
15
writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
16
}
17
else
18
{
19
writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
20
}
21
base.Render(writer);
22
}
23
}
24
25
internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
26
{
27
private string _url; // 假的URL
28
29
internal FormFixerHtml32TextWriter(TextWriter writer):base(writer)
30
{
31
_url = HttpContext.Current.Request.RawUrl;
32
}
33
34
public override void WriteAttribute(string name, string value, bool encode)
35
{
36
if (_url != null && string.Compare(name, "action", true) == 0)
37
{
38
value = _url;
39
}
40
base.WriteAttribute(name, value, encode);
41
42
}
43
}
44
45
internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
46

{
47
private string _url;
48
internal FormFixerHtmlTextWriter(TextWriter writer):base(writer)
49
{
50
_url = HttpContext.Current.Request.RawUrl;
51
}
52
53
public override void WriteAttribute(string name, string value, bool encode)
54
{
55
if (_url != null && string.Compare(name, "action", true) == 0)
56
{
57
value = _url;
58
}
59
base.WriteAttribute(name, value, encode);
60
}
61
}
62
63
}
64
65
把这个文件编译成dll,并在你的项目中引用它。
然后把项目中的所有aspx文件对应的cs文件中的继承page类的代码改写为继承OLPage。
例如
public class WebForm1:page
改写为
public class WebForm1:URL.OLPage
这样就解决回发问题。