A known bug in ASP.NET 1.x and 2.0 is that the action attribute of a form doesn’t respect URL rewrites. Everyone that uses URL rewrites uses one of several mechanisms to take care of the action attribute bug.
Recently it has been quite popular to use control adapters and .browser files to change the rendering of a form and its attributes. I’ve never liked that approach because it treats the symptom instead of fixing the problem in a transparent and clean way.
The use of control adapters and .browser files to alter the output for different clients is just as bad as having separate stylesheet files for IE and Firefox. Instead of writing cross-browser mark-up and CSS you treat the browser differences as an illness and try to treat it. The thing is that you don’t cure it by using control adapters; you just treat the symptom of not being able to (or don’t care to) write cross-browser code.
However, it still has its right when dealing with mobile devices, but not much longer. The browser in the iPhone or Windows Mobile is becoming so good that it soon doesn’t matter anymore.
In the past I’ve used a custom overridden HtmlForm but when I had to do it recently on my job, I thought it was about time to make it better and cleaner. I very much like the use of a custom form control approach because it is very transparent in what it does and where it does it.
/// A custom HtmlForm for use when a postback is made to a website
/// that has URL rewriting enabled.
public class RewriteForm : HtmlForm
/// Renders the attributes using the RewriteFormHtmlTextWriter.
protected override void RenderAttributes(HtmlTextWriter writer)
RewriteFormHtmlTextWriter custom = new RewriteFormHtmlTextWriter(writer);
/// Writes the HtmlForm's markup to support URL rewrites.
private class RewriteFormHtmlTextWriter : HtmlTextWriter
/// Initializes a new instance of the <see cref="RewriteFormHtmlTextWriter"/> class.
/// <param name="writer">The writer.</param>
public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
base.InnerWriter = writer.InnerWriter;
/// Writes all attributes the normal way, but writes the action attribute differently.
/// <param name="name">The markup attribute to write to the output stream.</param>
/// <param name="value">The value assigned to the attribute.</param>
/// <param name="fEncode">true to encode the attribute and its assigned value; otherwise, false.</param>
public override void WriteAttribute(string name, string value, bool fEncode)
if (name == "action")
HttpContext context = HttpContext.Current;
if (context.Items["ActionAlreadyWritten"] == null)
value = context.Request.RawUrl;
context.Items["ActionAlreadyWritten"] = true;
base.WriteAttribute(name, value, fEncode);