Adding Routing to an otherwise mostly static asp.net web site in IIS6

5 Oktober 2009

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


www.twee.net/bands/band.asp?key=smiths

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

Now you can get to the same page using

www.twee.net/band/The 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

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


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
              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)
    {
            RegisterRoutes(RouteTable.Routes);
    }
    
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new System.Web.Routing.Route
        (
             "band/{bandname}"
             , 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 ASP.net 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 asp.net runtime. So all requests under /band/ will go to asp.net.

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

Categories

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