UPDATES:
- Updated and simplified sample project.
- Updated original post with IIS7 example here.
- 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.
- Updated sample and overview of using SiteMaps with routing here.
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.


19 comments
Comments feed for this article
May 5, 2008 at 9:46 pm
ASP.NET Routing… Goodbye URL rewriting? « Chris Cavanagh’s Blog
[...] March 11, 2008 · 4 Comments UPDATE 2: You can find a sample project and more details here. [...]
June 4, 2008 at 7:41 am
James McConnell
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
June 4, 2008 at 8:30 am
Chris Cavanagh
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!
June 24, 2008 at 9:05 pm
Zac
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?
June 24, 2008 at 10:40 pm
Chris Cavanagh
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.
June 25, 2008 at 6:28 am
Zac
Woah that was it – thanks!!!!
August 22, 2008 at 12:11 am
Pacc
Hi, nice post. Your demo works fine but when I try to actual example and build it, it gives me 404 error. I have added UrlRoutingModule httpModule to web.config etc.. Do you have any idea what could be the problem? Thanks!
August 22, 2008 at 12:47 am
Chris Cavanagh
Pacc – Are you using the VS.NET webserver or IIS? If IIS, be sure you’ve set up the “wildcard mapping” mentioned above. Also drop a breakpoint in Global.asax.cs and make sure you’re getting in there (Application_Start won’t necessarily run every time you debug, depending on the webserver you’re using). Let me know how far you get…
October 7, 2008 at 2:28 am
DM
I’m having a difficult time to get this working with IIS7 Integrated mode. I’m guessing the problem lies in the web.config file. How do you manage that?
October 8, 2008 at 1:01 am
Chris Cavanagh
DM – I’ve updated the original post with some IIS7 details. Hope it’s useful!
http://chriscavanagh.wordpress.com/2008/03/11/aspnet-routing-goodbye-url-rewriting/
October 17, 2008 at 3:15 am
shikha
your demo works fine but when i try it on my actula project, it gives me 404 error. i m not using iis ,i m using vs.net as server
Do you have any idea what could be the problem?
October 17, 2008 at 6:32 am
Chris Cavanagh
shikha – Make sure Default.aspx is set as the startup page in VS (or add /Default.aspx to the url in the browser). FYI it’s only using Default.aspx as a demo launch page, to show a couple typical URLs to try.
Hope this helps!
October 18, 2008 at 12:03 am
shikha
hi
Thanks for replying.
i made the default.aspx as starup page and give a link to the next page……it was working…
but
as my all aspx pages are in folder named LOGGED.
When i use same pattern from the next page that is come up from Default .again it gives me 404error:
Example:From Default page i go to the Index.aspx thats is in LOGGEd Folder.
On Link..it goes to the
INdex page………
But whaen i use and it should go to the link–http://localhost../Logged/Movies.aspx….But it doesnt i gives me 404 exception.
plz suggest why it is happening…
October 18, 2008 at 1:20 am
Chris Cavanagh
Shikha – Could you send a zip of your files to blog@chriscavanagh.com? (or a quick sample project to demonstrate the problem). I’ll take a look and let you know
October 18, 2008 at 2:03 am
shikha
thank you for your help.
-I f i want to send dynamic values in a anchor like
href=”123″…and this value is changing accordingly it can be 123 or 23…anythng……..so how to map path for that.
I m doing like
var detailsHandler2 = new WebFormRouteHandler(“~/Log/id.aspx”);
routes.Add(new Route(“{value}”, detailsHandler2));
and the value here is “123″………….it gives me 404
.So plz suggest me how to map path for in case of dynamic values
October 18, 2008 at 4:26 pm
Chris Cavanagh
Shikha – Take a look at the updated sample project here: http://chriscavanagh.wordpress.com/2008/10/18/aspnet-routing-revisited-again/
It includes an example of dynamic URL generation based on defined routes. It’s also generally simplified; hopefully that’ll make it easier to compare to other projects to diagnose problems.
Let me know if this helps
July 28, 2009 at 3:38 pm
Scott
Has anyone had any luck using Routing with WebForms and being able to process web.config files in a subdirectory for which there is no route mapped to?
August 11, 2009 at 2:35 pm
Royce
How do you configure this to work with IIS 5
December 22, 2009 at 6:32 am
Routing ASP.NET | ЧПУ в ASP.NET | Создание "красивых" урл-адресов в ASP.NET | WEBFeria
[...] http://chriscavanagh.wordpress.com/2008/10/18/aspnet-routing-revisited-again/ http://chriscavanagh.wordpress.com/2008/04/25/systemwebrouting-with-webforms-sample/ http://chriscavanagh.wordpress.com/2008/11/06/aspnet-routing-just-enough-rope/ [...]