Subsite Rewriting HttpModule for ASP.NET

Nov 3, 2007  • ASP.NET  • .NET

Here’s some simple code for easily adding Subsite functionality to an ASP.NET website using UrlRewriting/UrlMapping.

Urls are rewritten in the following fashion:

~/jdoe/default.aspx => ~/default.aspx?site=jdoe ~/jdoe/subfolder/default.aspx => ~/subfolder/default.aspx?site=jdoe

And this is all done by the 65 line HttpModule below:


using System;
 using System.Web;

/// <summary>
 /// Summary description for SubsiteRewriteModule
 /// </summary>
 public class SubsiteRewriteModule : IHttpModule
 {
     #region IHttpModule Members

    void IHttpModule.Dispose() { }

    void IHttpModule.Init(HttpApplication context)
     {
         context.BeginRequest += new EventHandler(context_BeginRequest);
     }

    #endregion

    private void context_BeginRequest(object sender, EventArgs e)
     {
         HttpContext context = ((HttpApplication)sender).Context;
         HttpRequest Request = context.Request;

        if (Request.Path.ToLowerInvariant().EndsWith(".aspx"))
         {
             string UrlToRewrite = VirtualPathUtility.ToAppRelative(Request.Path).ToLowerInvariant();
             bool doRewrite = false;
             System.Text.RegularExpressions.Regex regex;


             regex = new System.Text.RegularExpressions.Regex("~/(\\w+)/(.*)");
             if (regex.Match(UrlToRewrite).Success)
             {
                 UrlToRewrite = regex.Replace(UrlToRewrite, "~/$2?site=$1");
                 doRewrite = true;
             }


             /// Rewrite the URL and inlude all querystring criteria so we don't lose it
             if (doRewrite)
             {
                 if (UrlToRewrite.Contains("?"))
                 {
                     UrlToRewrite = UrlToRewrite + "&amp;" + GetQueryString(context);
                 }
                 else
                 {
                     UrlToRewrite = UrlToRewrite + "?" + GetQueryString(context);
                 }
                 context.RewritePath(UrlToRewrite, false);
             }
         }

    }

    private static string GetQueryString(HttpContext context)
     {
         string returnVal = context.Request.QueryString.ToString();
         if (string.IsNullOrEmpty(returnVal))
             return "";
         else
             return returnVal;
     }
 }

Download a sample site using the above HttpModule here