Url 重写
1. 利用Intelligencia.UrlRewriter.dll
(1)在webconfig配置如下
<!--设置地址重写组件-->
<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<!--地址重写规则-->
<rewriter file="~/App_Data/rewrite.xml">
</rewriter>
<httpModules>
<add type="Intelligencia.UrlRewriter.RewriterHttpModule,Intelligencia.UrlRewriter" name="UrlRewriter" />
</httpModules>
(2)配置重写规则
<rewriteRules>
<!--外层-->
<rewrite url="~/Default.do" to="~/Default.aspx" processing="stop" />
</rewriteRules>
2. 利用 UrlRewriter.dll
(1)在webconfig配置如下
<section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter"/>
</configSections>
<CustomConfiguration>
<urls>
<add virtualUrl="~/Login.do" destinationUrl="~/Login.aspx"/>
</urls>
</CustomConfiguration>
<add type="URLRewriter.RewriterModule, URLRewriter" name="RewriterModule"/>
</httpModules>
以上都必须解决页面回发的问题
第一种方法:
添加 Form.browser
可在 <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers 中找到现有的浏览器定义
-->
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="Sample.Web.UI.Adapters.FormRewriterControlAdapter" />
</controlAdapters>
</browser>
</browsers>
添加类FormRewriterControlAdapter.cs
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Sample.Web.UI.Adapters
{
/// <summary>
/// 该类处理url重写后页面回发的问题
/// </summary>
public class FormRewriterControlAdapter :System.Web.UI.Adapters.ControlAdapter
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(new RewriteFormHtmlTextWriter(writer));
}
}
public class RewriteFormHtmlTextWriter : HtmlTextWriter
{
public RewriteFormHtmlTextWriter(HtmlTextWriter writer): base(writer)
{
this.InnerWriter = writer.InnerWriter;
}
public RewriteFormHtmlTextWriter(System.IO.TextWriter writer): base(writer)
{
this.InnerWriter = writer;
}
public override void WriteAttribute(string name, string value, bool fEncode)
{
if (name == "action")
{
HttpContext context = HttpContext.Current;
if (context.Items["ActionAlreadyWritten"] == null)
{
value = context.Request.RawUrl;
context.Items["ActionAlreadyWritten"] = true;
}
}
base.WriteAttribute(name, value, fEncode);
}
}
}
第二种方法 需要多调用一个 ActionlessForm.dll
添加 BrowserFile.browser
可在 <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers 中找到现有的浏览器定义
-->
<browsers>
<browser id="NewBrowser" parentID="Mozilla">
<identification>
<userAgent match="Unique User Agent Regular Expression" />
</identification>
<capture>
<userAgent match="NewBrowser (?'version'\d+\.\d+)" />
</capture>
<capabilities>
<capability name="browser" value="My New Browser" />
<capability name="version" value="${version}" />
</capabilities>
</browser>
<browser refID="Mozilla">
<capabilities>
<capability name="xml" value="true" />
</capabilities>
</browser>
</browsers>
让模板页继承一个Form 模板页需添加的代码为
<skm:Form ID="Form2" method="post" runat="server" enctype="multipart/form-data">
</skm:Form>
这样做一个坏处就是拆分的时候您看不到内容。
另外要给IIS添加相应的配置 。
注: 例子
参考:1.Intelligencia.UrlRewriter相关资源整理
3.10 补充:还有一个问题必须提到的就是重写规则的配置:
(1)如果您的.css .js 等文件无法访问,可以添加上
<rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.css|\.js)(\?.+)?)$" to="$1" processing="stop" />
(2)记得在配置重写规则的时候添加上 $ 终止符
如: <rewrite url="~/(\d)$" to="~/MySpace/Space/Space.aspx?UserId=$1" processing="stop" />
我原来配置为 <rewrite url="~/(\d)" to="~/MySpace/Space/Space.aspx?UserId=$1" processing="stop" />
发现当访问 http://www.dudu.com/481 与 http://www.dudu.com/sdfds/481 都可以指定到 Space.aspx。