1.添加引用
2.配置webconfig
webconfig
1<?xml version="1.0" encoding="utf-8"?>
2<configuration>
3
4 <configSections>
5 <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
6 </configSections>
7
8 <RewriterConfig>
9 <Rules>
10 <RewriterRule>
11 <LookFor>~/News/(\d+)\.aspx</LookFor>
12 <SendTo>~/default.aspx?id=$1</SendTo>
13 </RewriterRule>
14 </Rules>
15 </RewriterConfig>
16
17 <system.web>
18
19 <httpHandlers>
20 <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
21 </httpHandlers>
22
23 <compilation debug="true" />
24
25 <authentication mode="Windows" />
26 </system.web>
27
28</configuration>
29
1<?xml version="1.0" encoding="utf-8"?>
2<configuration>
3
4 <configSections>
5 <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
6 </configSections>
7
8 <RewriterConfig>
9 <Rules>
10 <RewriterRule>
11 <LookFor>~/News/(\d+)\.aspx</LookFor>
12 <SendTo>~/default.aspx?id=$1</SendTo>
13 </RewriterRule>
14 </Rules>
15 </RewriterConfig>
16
17 <system.web>
18
19 <httpHandlers>
20 <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
21 </httpHandlers>
22
23 <compilation debug="true" />
24
25 <authentication mode="Windows" />
26 </system.web>
27
28</configuration>
29
3.测试
测试
1public partial class _Default : System.Web.UI.Page
2{
3 public string id;
4 protected void Page_Load(object sender, EventArgs e)
5 {
6 id = Request.QueryString["id"].ToString();
7 }
8 protected void Button1_Click(object sender, EventArgs e)
9 {
10 Response.Write(GetCategory());
11 }
12
13 string GetCategory()
14 {
15 if (Request.PathInfo.Length == 0)
16 {
17 return "";
18 }
19 else
20 {
21 return Request.PathInfo.Substring(1);
22 }
23 }
24}
1public partial class _Default : System.Web.UI.Page
2{
3 public string id;
4 protected void Page_Load(object sender, EventArgs e)
5 {
6 id = Request.QueryString["id"].ToString();
7 }
8 protected void Button1_Click(object sender, EventArgs e)
9 {
10 Response.Write(GetCategory());
11 }
12
13 string GetCategory()
14 {
15 if (Request.PathInfo.Length == 0)
16 {
17 return "";
18 }
19 else
20 {
21 return Request.PathInfo.Substring(1);
22 }
23 }
24}
最后,发现重写的目的达到了。但是,单击button时路径暴露了。
接着查资料...