Asp.net url重写后页面回发地址改变的处理
在Url重写的页面中进行PostBack并且不跳转的情况,会导致URL变回URL重写前的原始URL,这样的体验不太好,但是这种请况可以通过以下方法进行处理:
在微软的URLRewriter类库中添加以下类之后编译后,然后在项目中引用该UrlRewriter.dll文件
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// FormRewriter 的摘要说明 /// </summary> namespace URLRewriter { public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter { public FormRewriterControlAdapter() { } protected override void Render(HtmlTextWriter writer) { base.Render(new RewriteFormHtmlTextWriter(writer)); } } public class RewriteFormHtmlTextWriter : HtmlTextWriter { public RewriteFormHtmlTextWriter(HtmlTextWriter writer) : base(writer) { base.InnerWriter = writer.InnerWriter; } public RewriteFormHtmlTextWriter(System.IO.TextWriter writer) : base(writer) { base.InnerWriter = writer; } public override void WriteAttribute(string name, string value, bool fEncode) { //If the attribute we are writing is the "action" attribute, and we are not on a sub-control, //then replace the value to write with the raw URL of the request - which ensures that we'll //preserve the PathInfo value on postback scenarios if (name == "action") { HttpContext context = HttpContext.Current; if (context.Items["ActionAlreadyWritten"] == null) { //We will use the Request.RawUrl property within ASP.NET to retrieve the origional //URL before it was re-written. value = context.Request.RawUrl; //Indicate that we've already rewritten the <form>'s action attribute to prevent //us from rewriting a sub-control under the <form> control context.Items["ActionAlreadyWritten"] = true; } } base.WriteAttribute(name, value, fEncode); } } }
URLRewriter类库代码可以在UrlRewriter下载
然后在App_Browsers文件夹下创建Form.browser
代码如下:
<!-- 可在 <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers 中找到现有的浏览器定义 --> <browsers> <browser refID="Default"> <controlAdapters> <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="URLRewriter.FormRewriterControlAdapter" /> </controlAdapters> </browser> <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>
在处理重写成html的时候本来网站中的html页面将会不能使用
使用以上方式将不存在这个个问题
如果还不行可以在<compilation debug="true">节点下添加
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
在<httpHandlers>节点下添加(如果之前使用的是http处理程序执行重写的,请写在前面)
<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>