如何在URL重写后,form标记中的Action值也用虚拟地址?

如何在URL重写后,form标记中的Action值也用虚拟地址?

例如我们将一个地址"showpost.aspx?postid=1000"重写为"posts/1000.aspx",如果这个页面中有个表单(form),那么输出到浏览器的HTML中,这个表单的action属性为<form action="showpost.aspx?postid=1000">而不是我们期望的<form action="1000.aspx">,这样就会暴露我们的真实地址,而且感觉也不爽。

我们可以通过自定义页面基类,重写默认的HtmlTextWriter方法,重写属性输出WriteAttribute(string name, string value, bool encode)方法,当输出action时,将action属性的值强制转换为当前虚URL(HttpContext.Current.Request.RawUrl,上面的1000.aspx)

最后就是让所有的页面都调用这个基类,修改一下Web.config就好了
<!-- Openlab.Controls.OLPage为页面基类,Openlab.Controls为基类所在dll -->
<pages pageBaseType="Openlab.Controls.OLPage, Openlab.Controls"/>

附页面基类完整代码(参考CS代码完成):

 
    
None.gif using System; None.gif using System.IO; None.gif using System.Web; None.gif using System.Web.UI; None.gifNone.gif namespace Openlab.Controls ExpandedBlockStart.gifContractedBlock.gif ... { ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary> InBlock.gif/// 页面基类 ExpandedSubBlockEnd.gif/// </summary> InBlock.gif public class OLPage : Page ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gifpublic OLPage() ExpandedSubBlockStart.gifContractedSubBlock.gif...{ ExpandedSubBlockEnd.gif } InBlock.gifContractedSubBlock.gifExpandedSubBlockStart.gifRender#region Render InBlock.gifExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary> InBlock.gif/// 重写默认的HtmlTextWriter方法,修改form标记中的value属性,使其值为重写的URL而不是真实URL。 InBlock.gif/// </summary> ExpandedSubBlockEnd.gif/// <param name="writer"></param> InBlock.gif protected override void Render(HtmlTextWriter writer) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gifInBlock.gifif (writer is System.Web.UI.Html32TextWriter) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gif writer = new FormFixerHtml32TextWriter(writer.InnerWriter); ExpandedSubBlockEnd.gif } InBlock.gifelse ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gif writer = new FormFixerHtmlTextWriter(writer.InnerWriter); ExpandedSubBlockEnd.gif } InBlock.gifInBlock.gifbase.Render(writer); ExpandedSubBlockEnd.gif } ExpandedSubBlockEnd.gif#endregion InBlock.gifInBlock.gifExpandedSubBlockEnd.gif } InBlock.gifContractedSubBlock.gifExpandedSubBlockStart.gifFormFixers#region FormFixers InBlock.gifContractedSubBlock.gifExpandedSubBlockStart.gifFormFixerHtml32TextWriter#region FormFixerHtml32TextWriter InBlock.gifinternal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gifprivate string _url; // 假的URL InBlock.gif InBlock.gifinternal FormFixerHtml32TextWriter(TextWriter writer):base(writer) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gif _url = HttpContext.Current.Request.RawUrl; ExpandedSubBlockEnd.gif } InBlock.gifInBlock.gifpublic override void WriteAttribute(string name, string value, bool encode) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gif// 如果当前输出的属性为form标记的action属性,则将其值替换为重写后的虚假URL InBlock.gif if (_url != null && string.Compare(name, "action", true) == 0) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gif value = _url; ExpandedSubBlockEnd.gif } InBlock.gifbase.WriteAttribute(name, value, encode); ExpandedSubBlockEnd.gif } ExpandedSubBlockEnd.gif } ExpandedSubBlockEnd.gif#endregion InBlock.gifContractedSubBlock.gifExpandedSubBlockStart.gifFormFixerHtmlTextWriter#region FormFixerHtmlTextWriter InBlock.gifinternal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gifprivate string _url; InBlock.gifinternal FormFixerHtmlTextWriter(TextWriter writer):base(writer) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gif _url = HttpContext.Current.Request.RawUrl; ExpandedSubBlockEnd.gif } InBlock.gifInBlock.gifpublic override void WriteAttribute(string name, string value, bool encode) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gifif (_url != null && string.Compare(name, "action", true) == 0) ExpandedSubBlockStart.gifContractedSubBlock.gif...{ InBlock.gif value = _url; ExpandedSubBlockEnd.gif } InBlock.gifInBlock.gifbase.WriteAttribute(name, value, encode); ExpandedSubBlockEnd.gif } ExpandedSubBlockEnd.gif } ExpandedSubBlockEnd.gif#endregion InBlock.gifExpandedSubBlockEnd.gif#endregion ExpandedBlockEnd.gif}
posted on 2006-09-08 14:51 Steveson 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Steveson/archive/2006/09/08/498789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值