asp.net url重寫後頁面回傳問題
來源:互聯網
上載者:User
具體辦法如下:可以通過自訂頁面基類,重寫預設的HtmlTextWriter方法,重寫屬性輸出WriteAttribute(string name, string value, bool encode)方法,當輸出action時,將action屬性的值強制轉換為當前虛URL(HttpContext.Current.Request.RawUrl,上面的~/showVisasAll_(\w+)\.aspx)
最後就是讓所有的頁面都調用這個基類,修改一下Web.config就好了
<!-- Openlab.Controls.OLPage為頁面基類,Openlab.Controls為基類所在dll -->
<pages pageBaseType="Openlab.Controls.OLPage, Openlab.Controls"/>
附頁面基類完整代碼(參考CS程式碼完成):
複製代碼 代碼如下:using System;
using System.IO;
using System.Web;
using System.Web.UI;
namespace Openlab.Controls
...{
/**//// <summary>
/// 頁面基類
/// </summary>
public class OLPage : Page
...{
public OLPage()
...{
}
Render#region Render
/**//// <summary>
/// 重寫預設的HtmlTextWriter方法,修改form標記中的value屬性,使其值為重寫的URL而不是真實URL。
/// </summary>
/// <param name="writer"></param>
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);
}
#endregion
}
FormFixers#region FormFixers
FormFixerHtml32TextWriter#region FormFixerHtml32TextWriter
internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
...{
private string _url; // 假的URL
internal FormFixerHtml32TextWriter(TextWriter writer):base(writer)
...{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
...{
// 如果當前輸出的屬性為form標記的action屬性,則將其值替換為重寫後的虛假URL
if (_url != null && string.Compare(name, "action", true) == 0)
...{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
#endregion
FormFixerHtmlTextWriter#region FormFixerHtmlTextWriter
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);
}
}
#endregion
#endregion
}