您的位置:首页 > 其它

URL重写时 form action还原成原始URL的解决方法

2007-08-30 20:36 477 查看
这个问题早在几年前就被解决了, 但还是记录一下 .来自Cyahoga

编写FormFixerHtmlTextWriter类

using System;

using System.IO;

using System.Web.UI;

namespace Cuyahoga.Web.Util

{

/// <summary>

/// Custom HtmlWriter to override the default action attribute of the form tag.

/// We want this when the page is called with a virtual url (rewritten).

/// Thanks to Jesse Ezell for this solution: http://weblogs.asp.net/jezell/archive/2004/03/15/90045.aspx
/// 解决URL重写时 修改action url

/// </summary>

public class FormFixerHtmlTextWriter : HtmlTextWriter

{

private bool _inForm = false;

private string _action;

public FormFixerHtmlTextWriter(TextWriter writer, string tabString, string action) : base(writer, tabString)

{

this._action = action;

}

public override void WriteBeginTag(string tagName)

{

this._inForm = (tagName.Equals("form"));

base.WriteBeginTag (tagName);

}

public override void WriteAttribute(string name, string value, bool fEncode)

{

if (this._inForm)

{

if (name.Equals("action"))

{

value = this._action;

}

}

base.WriteAttribute (name, value, fEncode);

}

}

}

创建一个Page的积累 使采用URL重写的页面都继承这个类.

重写该类的render方法 便可以了

protected override void Render(System.Web.UI.HtmlTextWriter writer)

{

if (Context.Items["VirtualUrl"] != null)

{

writer = new FormFixerHtmlTextWriter(writer.InnerWriter, "", Context.Items["VirtualUrl"].ToString());

}

base.Render (writer);

}

这里的Context.Items["VirtualUrl"]代表原始链接的URL地址, 即context.Request.RawUrl , 需要将重写后的url改变回去. 如:section.aspx?id=1改成section-1.aspx.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: