:::: MENU ::::

Thursday, October 9, 2008

If you use URL rewriting in your ASP.NET apps you’re probably familiar with the workarounds required to get your rewritten URL to show up in the action attribute of the form on your pages (see ScottGu’s ‘Handling ASP.NET PostBacks with URL Rewriting’). Well, no more.

Included in the latest ASP.NET Service Pack is this gem (via Scott Hanselman):

HtmlForm.Action is now settable - Again, subtle, but very cool.  I like to use URL rewriting a lot and want my <form action=”"> to be a certain way. Now I can set it manually without fooling around with RegEx’s and messing with the whole response.

Woot! I deleted my FormRewriter.cs control adapter and .browser file then added this little snippet to my common BasePage (you can do this in either Page_Init or Page_Load on individual pages) and now my form action attributes reflect the original URL.

// header is created by Ionic’s ISAPI Rewrite Filter (IIRF)
string u = Request.Headers["X-Rewrite-Url"];
if (!string.IsNullOrEmpty(u))
{
   
Form.Action = u;
}

Be sure to check out the rest of Hanselman’s post for more details on what’s new in SP1.

More