asp.net url重写后页面回传问题
程序员文章站
2024-03-08 20:39:46
具体办法如下:可以通过自定义页面基类,重写默认的htmltextwriter方法,重写属性输出writeattribute(string name, string valu...
具体办法如下:可以通过自定义页面基类,重写默认的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
}
最后就是让所有的页面都调用这个基类,修改一下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
}