How to get URL rewriting to work with ASP.Net 2.0 Themes and form post-backs

Back in February, Scott Guthrie posted an article on URL rewriting with ASP.Net. Towards the end of the article there is a section on how to handle form post backs by sub classing HtmlForm (for ASP.Net 1.0 and 1.1) or by using a Control Adapter (for ASP.Net 2.0).

That seemed like a lot of work to get a viable solution so I decided to try and find a simpler solution before I “bit the bullet”. Additionally, ASP.Net 2.0 Themes do not always work with URL Rewriting and the article did not provide a solution.

The solution is to use the Application PostMapRequestHandler event to rewrite back to the original URL after the request has been mapped to its handler.

Following is an example that assume you are using ISAPI_Rewrite. ISAPI_Rewrite puts the originally requested URL in a requested header named HTTP_X_REWRITE_URL.

void Application_PostMapRequestHandler(object sender, EventArgs e)
{
    string originalUrl = Request.ServerVariables["HTTP_X_REWRITE_URL"];

    if (!string.IsNullOrEmpty(originalUrl))
    {
        Context.RewritePath(originalUrl);
    }
}

I have tested this method and have it working with extension-less URLs for both form post backs and ASP.Net 2.0 Themes.

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*