Troubleshooting ASP.NET MVC Routing

One thing I absolutely love about the ASP.NET MVC framework is that there isn't too much "magic". Magic can be nice if you are able to adhere to a framework's "Golden Path", but as soon as some customization becomes necessary you find that the magic can get in the way (DataContractSerializer anyone?).

One of the places in the ASP.NET MVC framework (and now part of .NET SP1) where magic is happening is routing. Routing takes a url, looks for the best fit from the pre-defined list of routes, breaks it up into parameters and feeds the whole thing to a handler. Pretty sweet, but if you have done something wrong it'll be tough to tell since all that is happening behind the scenes.

I've created a personal checklist to follow when I start to have routing troubles, if you have any additional steps you've found useful please post them in the comments and I will try to keep this checklist up to date. This article covers troubleshooting routes when using the ASP.NET MVC framework, however some of the steps mentioned will be applicable no matter how you are using routing.

1. Verify your controllers and actions.

I'm a contract first developer. I'll typically define my routes and views before I develop the controllers to use them. It's happened more than once that I started testing before I had written the method in my controller that I'm trying to access.

2. Look for duplicate routes.

This may not be as obvious as it seems. It's easy to create duplicate routes that read different but are hard for the routing system to distinguish. Remember that every parameter in your route is basically an empty slot where some data can be, so if you have 2 routes with the same number of slots in the same locations, they will be indistinguishable by the routing system.

3. Make sure your most specific routes are first.

Routes are tested against the URL in the order that they were added. If there is a less specific route that your URL can "fit" into before the one you actually wanted to execute, the routing system will choose that one and never get to your more specific route.

These 3 steps have helped me to successfully solve every routing issue I've come across so far, but if you have any other tips, I'd love to hear them.

Published Tuesday, September 09, 2008 10:33 PM by fdumlao
Filed under: , , ,

Comments

# re: Troubleshooting ASP.NET MVC Routing

Wednesday, September 10, 2008 7:59 AM by Mohamed Meligy

4- Check Phil Haack's ASP.NET Routing Debugger

See haacked.com/.../url-routing-debugger.aspx

# re: Troubleshooting ASP.NET MVC Routing

Wednesday, September 10, 2008 8:11 AM by fdumlao

@ Mohamed Meligy

That looks like an excellent tool - I'll have to check that out.

# ASP.NET MVC Archived Blog Posts, Page 1

Wednesday, September 10, 2008 9:48 AM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# re: Troubleshooting ASP.NET MVC Routing

Wednesday, September 10, 2008 11:06 AM by Elijah Manor

Phil's Route Debugger is cool, but Stephen Walther goes a little bit further with it... a little more interactive.

weblogs.asp.net/.../asp-net-mvc-tip-29-build-a-controller-to-debug-your-custom-routes.aspx

# re: Troubleshooting ASP.NET MVC Routing

Monday, November 03, 2008 10:18 AM by Rem

The blog is helpfull...

visit also <a href="http://asp-net-example.blogspot.com"><b>asp.net example</b></a>

# re: Troubleshooting ASP.NET MVC Routing

Sunday, May 10, 2009 10:08 AM by Mookies

I 've loved these RouteDebuger. I startedd with the haack'one then I 've prefer the sthephen's

But I have migrated on the RTM since I do not find any smart way to get all the information without using the Reflection... Any idea ?

# re: Troubleshooting ASP.NET MVC Routing

Sunday, May 10, 2009 6:27 PM by fdumlao

@Mookies:

I've actually been testing my routes in my unit tests for a while now and I have to say that it has helped to solve my route issues for good. take a look here: flux88.com/.../fluent-route-testing-in-asp-net-mvc

Leave a Comment

(required) 
(required) 
(optional)
(required)