Adding Routing to an otherwise mostly static web site in IIS6

5 October 2009

On TweeNet to access a band page, the old url is

You have to know the internal name of the band as well.

Now you can get to the same page using Smiths

The site runs under ASP.Net 3.5 SP1 on IIS6, so I have all the routing goodness of that ASP.Net version.
To get this working, I first had to add the routing assemblies and modules to my web.config

        <add assembly="System.Web.Abstractions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, 
             Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" name="UrlRoutingModule" />

These are additions to the already existing assemblies and modules which are not listed here.

Next I implemented a routing handler:

using System;
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;

namespace TweeNet.PublicSite.RouteHandlers
    public class BandRouteHandler : IRouteHandler
        public BandRouteHandler(string virtualPath)
            this.VirtualPath = virtualPath;

        public string VirtualPath { get; private set; }

        public IHttpHandler GetHttpHandler(RequestContext

            foreach (var urlParm in requestContext.RouteData.Values)
                requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;

            var page = BuildManager.CreateInstanceFromVirtualPath
                 (VirtualPath, typeof(Page)) as IHttpHandler;
            return page;

then in Global.asax I added the route:

    protected void Application_Start(Object sender, EventArgs e)
    public static void RegisterRoutes(RouteCollection routes)
        routes.Add(new System.Web.Routing.Route
             , new TweeNet.PublicSite.RouteHandlers.BandRouteHandler("~/bands/band.aspx")

In the code behind of the band.aspx page I added some code:

    if (HttpContext.Current.Items["bandname"] != null)
        string userTerm = HttpContext.Current.Items["bandname"].ToString();

        // now use that term to find the band data in the database and display the page content

The last problem was that IIS6 doesn't pass something like "The Smiths" to the pipeline. Steve Sanderson's blog post describes some options how to deal with this 

But in my situation the best option was the following:
As a /band directory did not existed yet, I created that empty folder in the filesystem.

Then in IIS I selected it and created an application for it. I now added a wildcard application mapping to the runtime. So all requests under /band/ will go to

All the other static files on the site are not affected. I then removed the application from '/band/'. IIS still keeps the wildcard mapping even if the folder is no longer an application and you can not see that mapping in the IIS Manager GUI.

Pages in this section


ASP.Net | Community | Development | IIS | IT Pro | Security | SQL (Server) | Tools | Web | Work on the road | Windows