ASP.NET 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: ,,

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.
Requesting Gravatar... Brandon May 27, 2008 10:10 PM
# re: ASP.NET Routing Debugger
Just wanted to say thanks. I was stuck; this unstuck me. You're the man, Phil. Thank you.
Requesting Gravatar... Christian Schiffer Jun 03, 2008 9:20 AM
# re: ASP.NET Routing Debugger
Hi thanks for this great tool. I tested it with preview 3 though and its not working, for some reason preveiw 3 of System.Web.Routing stated its version number to be 0.0.0.0 and obviously that there is a version conflict between my project and your dll.

Will you do an update anytime soon?
Requesting Gravatar... Some One Jun 03, 2008 5:35 PM
# re: ASP.NET Routing Debugger
We kinda get the source code. One area we don't is with the Routing HTTP Module. This along with reflector helps explain what is going on inside system.web.routing. Thanx.
Requesting Gravatar... Billy Ho Jun 16, 2008 8:16 PM
# re: ASP.NET Routing Debugger
To make the RouteDebugger works with ASP.Net MVC Preview 3.

All you have to do are the following items:

1. Obtain the source code from the path below:
http://haacked.com/code/RouteTesterDemo.zip

2. Replace the follow assemblies in the \RouteTesterDemo\Dependencies directory with their ASP.Net MVC Preview 3 counterparts:
System.Web.Abstractions.dll
System.Web.Mvc.dll
System.Web.Routing.dll

3. Rebuild the RouteDebug project.

The compiled assemblies which works with ASP.Net MVC Preview 3 can be obtained from the following path:
\RouteTesterDemo\RouteTester\bin\Debug\RouteDebug.dll



To make the sample project RouteTesterDemoWeb works with ASP.Net MVC Preview 3, a few addition steps are required:
1. In HomeController.cs
Modify:
RenderView("Index");
To:
View("Index");
2. In Web.Config
Repalce all:
"System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
With:
"System.Web.Routing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
3. In Web.Config
Replace all:
"System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
With:
"System.Web.Abstractions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
4. Rebuild the solution
5. Launch your favourite web browser and test with the URL like below:
http://localhost:14230/Home/Index
(replace the port number 14230 to the one your Visual Studio.Net is using)

Requesting Gravatar... Richard Dingwall Jun 28, 2008 11:56 PM
# re: ASP.NET Routing Debugger
Thanks Billy - your instructions worked like a charm. I've uploaded updated ASP.NET MVC Preview 3 versions for anyone in a hurry: grab routedebug-preview3-binary.zip or routetesterdemo-preview3.zip. Cheers.
Requesting Gravatar... Simone Jul 01, 2008 2:41 PM
# re: ASP.NET Routing Debugger
I remember someone posted a upgraded version of this tool that allowed switching on and off with a simple query string parameter, but cannot find it anymore.
Requesting Gravatar... Martin From Jul 25, 2008 8:02 PM
# re: ASP.NET Routing Debugger
Hey Phil and visitors,

i have created this VS Solution that includes both the RouteDebugger, the RouteEvaluator, Demo Web and Tests.

It is working with MVC Preview 4.

Havent really changed any code except for marking a few classes internal.

I have included a comment of what to do/remember, to make the Route Test work if implementing it in an existing solution.

It can be downloaded here dc60.2shared.com/.../RouteHelper.zip


Hope it can be of any use for someone out there.


Martin From
Requesting Gravatar... Peter Kellner Sep 10, 2008 10:54 AM
# re: ASP.NET Routing Debugger
not quite working with preview 5. when I download source, copy in 3 preview 5 dll's, build (no errors), run ..localhost/Home I get the errors:

I'm hoping this helps strangeness. I create simple controller, simple view, and sometimes, the dots just do not connect and I get a blank page. Day 2 for me on MVC so it's probably me somehow.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 125:
Line 126: string display = string.Empty;
Line 127: foreach (string key in values.Keys)
Line 128: display += string.Format("{0} = {1}, ", key, values[key]);
Line 129: if (display.EndsWith(", "))


Source File: E:\temp\rtnew\RouteTesterDemo\RouteTester\DebugHttpHandler.cs Line: 127
Requesting Gravatar... perkins Sep 12, 2008 1:30 AM
# re: ASP.NET Routing Debugger
I also meet the same problems! Haacked,Can you change the version to P5.Or could Anyone send a copy to me.Thanks
Requesting Gravatar... haacked Sep 12, 2008 11:16 AM
# re: ASP.NET Routing Debugger
I updated the source to P5. :)
Requesting Gravatar... Darryl Sep 12, 2008 9:06 PM
# re: ASP.NET Routing Debugger
It still seems broken with Preview 5. It complains that DebugHttpHandler needs to be derived from MvcHandler.

I don't know enough about MVC internals right now to fix it up. Making DebugHttpHandler derive from MvcHandler was not enough, now I don't get a crash but I also don't get any route tracing.

Or maybe I am doing something else incorrectly.
Requesting Gravatar... Corin Sep 16, 2008 9:02 AM
# re: ASP.NET Routing Debugger
Hi Phil,

Do you want to remove the binary, or update that to preview 5. Judging from the comment above, it seems people are still trying to download this instead of the source.

Requesting Gravatar... haacked Sep 16, 2008 9:47 AM
# re: ASP.NET Routing Debugger
I updated the binary. Sorry about that, I forgot that this post had two downloads.
Requesting Gravatar... darryl Sep 16, 2008 10:10 AM
# re: ASP.NET Routing Debugger
Ok,

I found my problem. When you create a new project in P5 it has the following code is the default.aspx code behind.

HttpContext.Current.RewritePath(Request.ApplicationPath);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);

If you comment out this code the route debugger works.

Otherwise it crashes on the third line with this exception

System.ArgumentException was unhandled by user code
Message="The MvcHttpHandler does not work with the handler of type 'RouteDebug.DebugHttpHandler' since it does not derive from MvcHandler.\r\nParameter name: httpHandler"
Source="System.Web.Mvc"
ParamName="httpHandler"
StackTrace:
at System.Web.Mvc.MvcHttpHandler.VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext)
at System.Web.Routing.UrlRoutingHandler.ProcessRequest(HttpContextBase httpContext)
at System.Web.Routing.UrlRoutingHandler.ProcessRequest(HttpContext httpContext)
at System.Web.Routing.UrlRoutingHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext context)
at mvctest._Default.Page_Load(Object sender, EventArgs e) in C:\Users\darryl\Documents\Visual Studio 2008\Projects\mvctest\mvctest\Default.aspx.cs:line 13
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
Requesting Gravatar... celik Sep 17, 2008 8:39 AM
# re: ASP.NET Routing Debugger
This is such a cool stuff. Thank you man for providing us with such a tool
Requesting Gravatar... Anthony Sep 23, 2008 8:58 AM
# re: ASP.NET Routing Debugger
It still seems broken with preview 5. I did the following:
* make a new asp.net MVC solution, build it
* Download the DLL
* Add a reference to routeDebug.dll to the solution
* Add the line to Application_Start
* run the application

- Get [ArgumentException: The MvcHttpHandler does not work with the handler of type 'RouteDebug.DebugHttpHandler' since it does not derive from MvcHandler.
Parameter name: httpHandler]

Commenting out the code in default.aspx (as suggested in the comments here) just produces a blank page.
Requesting Gravatar... Koistya `Navin Sep 26, 2008 2:10 AM
# re: ASP.NET Routing Debugger
Yeah, usefull stuff, I like it. Thanks for shareing, Phil.
Requesting Gravatar... balaji Oct 07, 2008 1:28 AM
# re: ASP.NET Routing Debugger
Hi,
I have downloaded the source and built the debugger using the MVC preview 5 and used it but still i am getting error.

MvcHttpHandler does not work with the handler of type RouteDebug.DebugHttpHandler' since it does not derive from MvcHandler.
Parameter name: httpHandler]

please help
Requesting Gravatar... hudo Oct 07, 2008 5:45 AM
# re: ASP.NET Routing Debugger
Just remove default.aspx, enter some valid url route or change code in defaut.aspx.cs to simple response.redirect. It crashes when redirecting request in default.aspx.cs, it seems they changed something in MvcHttpHanlder.ProcessRequest.
Requesting Gravatar... Jerry Oct 13, 2008 9:45 AM
# re: ASP.NET Routing Debugger
Hi hudo,
I am having the same problem as balaji and I have tried removing the default.aspx to no avail. I am trying to integrate this with an existing application. When removing the routeDebug statement in the gobal.asax I get an error saying the request does not match any route. When I run the route debugger as a stan alone applation and put the route I'm trying to debug in it works.

Please help.
Requesting Gravatar... Michael Stum Nov 09, 2008 4:46 PM
# re: ASP.NET Routing Debugger
Excellent stuff! any plans making it an integral part of ASP.net MVC? This really is a must-have Debugging Tool whenever someone wants to use Routing.
Requesting Gravatar... Ross Nov 12, 2008 7:44 AM
# re: ASP.NET Routing Debugger
Nice work. Just what I was after!
Requesting Gravatar... Jonathan Parker Mar 13, 2009 11:05 PM
# re: ASP.NET Routing Debugger
Very useful. Thanks. Would be good if it had a way of enabling/disabling without having to recompile or restart the site.
Requesting Gravatar... Eric Mar 16, 2009 1:53 PM
# re: ASP.NET Routing Debugger
I've just started an MVC project and definitely had some confusion about routes. This helped me understand which route was getting matched and why. Thanks so much!
Requesting Gravatar... mohamad Apr 21, 2009 6:36 AM
# re: ASP.NET Routing Debugger
hi
i want write twiter like routing in ASP.NET MVC.
for example:
in http://twitter.com/mammadTavakoli mammadTavakoli is the user name
in http://twitter.com/mammadTavakoli/friends friends come befor of a user name
and the twiter have himself pages that dont conflict whit user name?
pls help me
Requesting Gravatar... Jagdeep Mankotia May 28, 2009 2:40 AM
# re: ASP.NET Routing Debugger
Greats Help full I really liked it.

Thanks.
Requesting Gravatar... Chris Jun 02, 2009 3:04 AM
# re: ASP.NET Routing Debugger
Just found this, but really could have done with this a couple of weeks ago! Doh!
Well, will be VERY useful in future. Cheers!
Requesting Gravatar... Sara Jun 14, 2009 8:57 PM
# re: ASP.NET Routing Debugger
Thanks for this, great tool.
Requesting Gravatar... David Jun 28, 2009 3:21 AM
# re: ASP.NET Routing Debugger
Nice... this is very helpful. thanks
Requesting Gravatar... tori Jul 01, 2009 2:55 AM
# re: ASP.NET Routing Debugger
Thanks for this. A must-have for a beginner like me.

What do you have to say?

(will show your gravatar)
Please add 7 and 4 and type the answer here: