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
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.