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>

4 Comments:

  • This is good solution but it seems that page caching stops to work

    By Blogger AVIvanov, at 6:53 PM  

  • where is [originalRequestUrlTag] declare..
    how can we use it?
    ----------
    Please write complete code or give a link for download complete project.

    Thanks in advance :)

    By Blogger ravi kant, at 4:07 PM  

  • Thanks a lot.
    But some syntax is missing.
    i corrected it. Below you will find which ones;


    below you have to change some code

    replace this one

    context.Items[originalRequestUrlTag] = context.Request.RawUrl;

    with

    context.Items["originalRequestUrlTag"] = context.Request.RawUrl;



    and replace this one

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


    with

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

    By Blogger Unknown, at 12:54 PM  

  • I personally believe that rewriting services is best for those who can't write well. The rewriteing services are best in all.

    By Blogger aliyaa, at 4:16 PM  

Post a Comment

<< Home

Created dolly