Chris Cavanagh’s Blog

System.Web.Routing with WebForms sample

April 25, 2008 · 6 Comments

UPDATE: You can see a sample site running here.  As it’s using IIS6 I needed to add a Wildcard Application Mapping.  Note this passes every request through ASP.NET; you might prefer some alternative options, or use IIS 7 where you get it for free.

Since my earlier post on using the System.Web.Routing assembly with traditional WebForms (here), I’ve had some requests for a sample project to show it in action.  You can get the source right here.

This sample shows a way of getting the route information from the RouteHandler to the page, with minimal changes to existing pages (good if you’re migrating from a 3rd party URL rewriter).  Basically the WebFormRouteHandler copies relevant routing values into the HttpContext.Items collection (so they’ll only exist for the lifetime of the current request).  You can then grab these values in your WebForm.

For another example of passing routing details to a WebForm, see Phil Haack’s great post here.

Categories: .NET

6 responses so far ↓

  • ASP.NET Routing… Goodbye URL rewriting? « Chris Cavanagh’s Blog // May 5, 2008 at 9:46 pm

    [...] March 11, 2008 · 4 Comments UPDATE 2: You can find a sample project and more details here. [...]

  • James McConnell // June 4, 2008 at 7:41 am

    Chris, stumbled across this blog post looking for using the new Routing feature with Web Forms, and I have to say thank you! I need something like this in a personal project I’m working on, and your sample shows just how easy it is. Thanks!

    James

  • Chris Cavanagh // June 4, 2008 at 8:30 am

    James - Glad to be of help! :) I’ve been using it a while now and found it to be robust and (just as importantly) a nice, clean solution. Even postbacks work! :)

  • Zac // June 24, 2008 at 9:05 pm

    This works great using the VS Dev Server, but I can’t get it to work when published to my IIS6 box with the 3.5 SP1 Beta installed. I think I’m not doing the Wild Card Mapping correctly, should I be adding “c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll” to my Wildcard Application Map?

  • Chris Cavanagh // June 24, 2008 at 10:40 pm

    Zac - Yep that’s it; be sure to uncheck “Verify that file exists” or it won’t work (IIS would see there’s no file and immediately deny the request). Let me know if still a problem and I’ll take a closer look at my setup.

  • Zac // June 25, 2008 at 6:28 am

    Woah that was it - thanks!!!!

Leave a Comment