ASP.NET 2.0 Resources

Powered by Blogger

URL Rewriting using ASP.NET 2.0 Http Module

This arcticle describes how to write your own Http module for URL rewriting. This solution works only for URL addresses that end with .aspx extension (or any other extension handled by ASP.NET 2.0 framework). It's possible to configure ASP.NET ISAPI filter to handle all request and rewrite all possible URL's, but it won't be covered in this post.

HTTP module is any class that implements IHttpModule interface that contains two simple methods.

public void Init(HttpApplication application) {}
public void Dispose() {)

The first method is called, when the Http Module is initialized. There is a parameter with currenct HttpApplication refference that is passed to this Init method. You can hook to application events using delegate method. First we will hook to Application BeginRequest event, that is fired before request is handled by Http handler (in our case it handled .aspx pages).

   
public void Init(HttpApplication application) {
   application.BeginRequest += new EventHandler(this.Application_BeginRequest);
   application.PreRequestHandlerExecute += 
       new EventHandler(Application_PreRequestHandlerExecute);
}
private void Application_BeginRequest(Object source, EventArgs e)
{
   // rewrite URL to physical path...
   HttpApplication application = (source as HttpApplication);
   if (application != null) {
      HttpContext context = application.Context;
      if (context.Request.RawUrl.IndexOf("rew") > 0)
      {
          context.Items[originalRequestUrlTag] = context.Request.RawUrl;
          context.RewritePath("~/Rewrited.aspx");
      }
   }
}

You will have to substitute Application_BeginRequest method body with your own rewrite logic. In this sample all request that contain string "rew" in URL are rewrited to page ~/Rewrited.aspx. We are also hooking PreRequestHandlerExecute event. This event is called just before control is taken by Http handler. We will check if the page will be handled by System.Web.UI.Page handler and if so, we will hook PagePreInit event where we rewrite back original URL (it's very important otherwise form postback would not work correctly).

private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    HttpApplication application = sender as HttpApplication;
    System.Web.UI.Page page = application.Context.CurrentHandler as System.Web.UI.Page;
    if (page != null)
    {
        page.PreInit += new EventHandler(Application_PagePreInit);
    }
}
static private void Application_PagePreInit(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;

    if (context.Items.Contains(originalRequestUrlTag))
    {
        // rewrite back to request Url
        context.RewritePath(context.Items[originalRequestUrlTag] as string);
    }
}

This solution is completely transparent and implemented externally using Http module. The last thig we will have to do is register our new handler in web.config file.

<system.web>
  <httpModules>
    <add name="UrlRewriteModule" type="Core.Rewrite.UrlRewriteModule"/>
  </httpModules>
</system.web>

6 Comments:

Post a Comment

<< Home

Created dolly