Url Routing Debugger

In Scott Hanselman’s wonderful talk at Mix, he demonstrated a simple little route tester I quickly put together.

Route Tester - Windows Internet Explorer (2)

This utility displays the route data pulled from the request of the current request in the address bar. So you can type in various URLs in the address bar to see which route matches. At the bottom, it shows a list of all defined routes in your application. This allows you to see which of your routes would match the current URL.

The reason this is useful is sometimes you expect one route to match, but another higher up the stack matches instead. This will show you that is happening. However, it doesn’t provide any information why that is happening. Hopefully we can do more to help that situation in the future.

To use this, simply download the following zip file and place the assembly inside of it into your bin folder. Then in your Global.asax.cs file add one line to the Application_Start method (in bold).

protected void Application_Start(object sender, EventArgs e)
{
  RegisterRoutes(RouteTable.Routes);
  RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}

This will update the route handler (IRouteHandler) of all your routes to use a DebugRouteHandler instead of whichever route handler you had previously specified for the route. It also adds a catch-all route to the end to make sure that the debugger always matches any request for the application.

I’m also making available the full source (using the word full makes it sound like there’s a lot, but there’s not all that much) and a demo app that makes use of this route tester. Let me know if this ends up being useful or not for you.

Technorati Tags: ,,
[ad] Free Bug Tracking & Project Management Software Axosoft’s OnTime 2007 allows software development teams to collaborate on software projects by tracking everything from defects to enhancements to helpdesk incidents in one easy-to-use database driven by an intuitive Windows, Web or VS.NET Integrated UI. Get a Free Single-User License ($200 Value!)

What others have said

Requesting Gravatar... Cleve Littlefield Mar 13, 2008 5:58 PM
# re: Url Routing Debugger
It would be nice if similar to the way you can view Page.Trace events in if you had this was built-in viewer but configurable to shut off if this is the production site.
Requesting Gravatar... [mRg] Mar 14, 2008 4:59 AM
# re: Url Routing Debugger
Awesome thanks Phil .. this will be very useful :)
Requesting Gravatar... Perry Mar 14, 2008 5:15 AM
# re: Url Routing Debugger
Thanks Phil!

When I saw this at Mix I was hoping you guys would release it even though it's not hard to do myself. I just felt this is one of those things to DRY ;)
Requesting Gravatar... Srikanth Mar 14, 2008 1:29 PM
# re: Url Routing Debugger
Thanks Phil. Happen to see two web.config in the demo app and got an error "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.".

http://forums.devarticles.com/net-development-43/section-registered-as-allowdefinition-machinetoapplication-beyond-application-level-10027.html

It seems the issue is, not to have two web.config inside the same solution, once i got rid of the web.config for the web app all golden!
Requesting Gravatar... Ben Scheirman Mar 14, 2008 11:44 PM
# re: Url Routing Debugger
Phil, any particular reason why this code accepts a route collection as a method parameter, but then goes ahead and accesses the RouteTable.Routes singleton directly?

Is this an oversight, or is there a reason you did this?

(I told Scott that we should call this BettererRouteHandler. Think about it...)
Requesting Gravatar... Haacked Mar 18, 2008 6:21 PM
# re: Url Routing Debugger
@Ben Yep, that's an oversight. Doh!
Requesting Gravatar... Haacked Mar 18, 2008 6:28 PM
# re: Url Routing Debugger
@Ben Fixed!
Requesting Gravatar... Damian Powell Mar 26, 2008 2:04 AM
# re: Url Routing Debugger
Nice one, Phil. As Cleve pointed out, it would be nice if this information was printed out *after* the normal processing of a page. If I get around to doing that, I'll send a copy back to you.

What do you have to say?

(will show your gravatar)
Please add 3 and 5 and type the answer here: