URL重写给 asp.net Ajax带来的问题
在使用了URL重写的页面,我们为了保证用户进行PostBack后页面的URL仍然是,我们原先展现给用户的链接,比如是list_1.html而不是list.aspx?page=1
我们需要使用app_browsers目录,存放浏览器定义文件,相关说明可以参考msdn:http://msdn2.microsoft.com/zh-cn/library/ms228122(vs.80).aspx
<!-- 可在 <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers 中找到现有的浏览器定义 --> <browsers> <browser refID="Default"> <controlAdapters> <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="FormRewriterControlAdapter" /> </controlAdapters> </browser> </browsers>
代码如下:
public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter { protected override void Render(HtmlTextWriter writer) { if (HttpContext.Current.Request.RawUrl.Contains(".html")) { base.Render(new RewriteFormHtmlTextWriter(writer)); } else { base.Render(writer); } } } public class RewriteFormHtmlTextWriter : HtmlTextWriter { public RewriteFormHtmlTextWriter(Html32TextWriter 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") { if (HttpContext.Current.Items["ActionAlreadyWritten"] == null) { value = HttpContext.Current.Request.RawUrl; HttpContext.Current.Items["ActionAlreadyWritten"] = true; } } base.WriteAttribute(name, value, fEncode); } }
以上代码一直运行了很长时间,但是后来在使用asp.net Ajax是发现URL重写后的页面会出现错误,重新修改代码,排除Asp.net Ajax请求进行处理,代码如下:
public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter { protected override void Render(HtmlTextWriter writer) { if (HttpContext.Current.Request.RawUrl.Contains(".html")) { //取得asp.net Ajax请求标志 var AsyncPost=HttpContext.Current.Request.Form["__ASYNCPOST"]; if (AsyncPost=="true") //判断是否asp.net Ajax请求 { base.Render(writer); } else { base.Render(new RewriteFormHtmlTextWriter(writer)); } } else { base.Render(writer); } } } public class RewriteFormHtmlTextWriter : HtmlTextWriter { public RewriteFormHtmlTextWriter(Html32TextWriter 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") { if (HttpContext.Current.Items["ActionAlreadyWritten"] == null) { value = HttpContext.Current.Request.RawUrl; HttpContext.Current.Items["ActionAlreadyWritten"] = true; } } base.WriteAttribute(name, value, fEncode); } }
后又经过调试发现是RewriteFormHtmlTextWriter的构造器有问题,没有包含htmlTextWriter类型的构造器,而使用了TextWriter类型构造器,导致页面结构错误出现null报错。再次调整代码如下:
public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter { protected override void Render(HtmlTextWriter writer) { if (HttpContext.Current.Request.RawUrl.Contains(".html")) { //使用过滤掉asp.net Ajax请求的方式避免错误 //而后发现是RewriteFormHtmlTextWriter的构造器的问题,遂放弃方法 //var AsyncPost=HttpContext.Current.Request.Form["__ASYNCPOST"]; //if (AsyncPost=="true") //{ // base.Render(writer); //} //else //{ // base.Render(new RewriteFormHtmlTextWriter(writer)); //} base.Render(new RewriteFormHtmlTextWriter(writer)); } else { base.Render(writer); } } } public class RewriteFormHtmlTextWriter : HtmlTextWriter { //使用html 4.0方式的渲染器 public RewriteFormHtmlTextWriter(HtmlTextWriter writer) : base(writer) { this.InnerWriter = writer.InnerWriter; } //使用html 3.2方式的渲染器,兼容老浏览器 public RewriteFormHtmlTextWriter(Html32TextWriter writer) : base(writer) { this.InnerWriter = writer.InnerWriter; } //其他方式浏览器,有可能引起null错误 public RewriteFormHtmlTextWriter(System.IO.TextWriter writer) : base(writer) { this.InnerWriter = writer; } public override void WriteAttribute(string name, string value, bool fEncode) { if (name == "action") { if (HttpContext.Current.Items["ActionAlreadyWritten"] == null) { value = HttpContext.Current.Request.RawUrl; HttpContext.Current.Items["ActionAlreadyWritten"] = true; } } base.WriteAttribute(name, value, fEncode); } }关于HtmlTextWriter和Html32TextWriter可以看一下这篇文章 http://www.cnblogs.com/Vegaslee/archive/2007/12/04/982723.html