ASP.NET MVC framework is another tools who webmasters everywhere be fond of, since it’s not offer you too many ‘surprises’. Those surprises are good only if you can handle the framework golden path. Once some customizations turn out to be required, you will find that those surprises are hindering.
In the ASP.NET MVC framework you can find one of those surprises takes place in routing. Most likely routing will take a url, seek the best suit from the pre-defined list routes, fragment it into parameters and feeds the whole thing to a handler. This is only good until you have not done anything wrong. If this happened and since all of these happen behind the scenes, you will need a lot of hard work to figure it out.
The followings are steps to follow if you experience routing troubles:
1. Verify your controllers and actions.
Define your routes and views before 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. Search 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.