Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



Subsite Rewriting HttpModule for ASP.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 + "&" + 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

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: asp.net | General
Posted by crpietschmann on Saturday, November 03, 2007 5:24 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann