使用URL重写可以实现用http://www.mzwu.com/d100.aspx类似的地址来访问http://www.mzwu.com /default.aspx?id=100,这样的好处非常多:一是隐藏了真实路径,提高应用程序的安全性;二是有利于搜索引擎收录;三是便于记忆。下边 我们来看看在ASP.NET中怎么实现URL重写。
1.下载URLRewriter.dll 放到Bin文件夹中。
2.Web.config中配置:
程序代码
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<!-- 规则1 -->
<RewriterRule>
<LookFor>~/d(/d+)/.aspx</LookFor>
<SendTo>~/default.aspx?id=$1</SendTo>
</RewriterRule>
<!-- 规则2 -->
<RewriterRule>
<LookFor>~/k(/d+)/.aspx</LookFor>
<SendTo>~/default.aspx?key=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
</system.web>
</configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<!-- 规则1 -->
<RewriterRule>
<LookFor>~/d(/d+)/.aspx</LookFor>
<SendTo>~/default.aspx?id=$1</SendTo>
</RewriterRule>
<!-- 规则2 -->
<RewriterRule>
<LookFor>~/k(/d+)/.aspx</LookFor>
<SendTo>~/default.aspx?key=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
</system.web>
</configuration>
OK, 这样就实现了URL重写,运行测试就能看到结果了。不过,这样还存在一个问题:当页面中进行回调时,URL会变成真实的地址 http://www.mzwu.com/default.aspx?id=100,所以我们得花点时间再来解决下这个问题,思路是新建个类 BasePage来专门处理这个问题,它继承自System.Web.UI.Page类,然后其他页面都改为继承自BasePage即 可,BasePage类内容:
程序代码
using System;
using System.Web;
using System.Web.UI;
using System.IO;
/// <summary>
///BasePage 的摘要说明
/// </summary>
public class BasePage : Page
{
public BasePage()
{ }
protected override void Render(HtmlTextWriter writer)
{
if (writer is System.Web.UI.Html32TextWriter)
{
writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
}
else
{
writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
}
base.Render(writer);
}
}
internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
{
private string _url;
internal FormFixerHtml32TextWriter(TextWriter writer)
: base(writer)
{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
{
if (_url != null && string.Compare(name, "action", true) == 0)
{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
{
private string _url;
internal FormFixerHtmlTextWriter(TextWriter writer)
: base(writer)
{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
{
if (_url != null && string.Compare(name, "action", true) == 0)
{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
using System.Web;
using System.Web.UI;
using System.IO;
/// <summary>
///BasePage 的摘要说明
/// </summary>
public class BasePage : Page
{
public BasePage()
{ }
protected override void Render(HtmlTextWriter writer)
{
if (writer is System.Web.UI.Html32TextWriter)
{
writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
}
else
{
writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
}
base.Render(writer);
}
}
internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
{
private string _url;
internal FormFixerHtml32TextWriter(TextWriter writer)
: base(writer)
{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
{
if (_url != null && string.Compare(name, "action", true) == 0)
{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
{
private string _url;
internal FormFixerHtmlTextWriter(TextWriter writer)
: base(writer)
{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
{
if (_url != null && string.Compare(name, "action", true) == 0)
{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}