Practical .NET

The Best Advice You'll Get on ASP.NET MVC Routing

Here are three rules for defining ASP.NET MVC routes that you can actually understand.

I wish I had a dollar for every client who's called me because they can't figure out why some URL associated with their ASP.NET MVC application isn't invoking the right action method (or why some parameter in the URL isn't being passed to the method). The number of tools that have appeared over the years to allow you to determine what URLs will invoke what methods in which controllers speaks to the depth of the problem. It's like the Monty Hall problem (which even Jamie and Adam from MythBusters struggled with): ASP.NET routing, like probability, appears to be something that the human mind doesn't easily grasp.

Rather than try to figure out routing, the right answer is to simplify your routing by creating routes that follow these three rules:

  1. Write your routes so that their templates match to URLs that begin with specific strings (like the default rule in the ASP.NET Web API, which requires all of its URLs to begin with the string "api").
  2. Remove your controller and action method names from your URLs and put them in your default values -- have only parameter values embedded in your URLs.
  3. Avoid optional and default values for items in your URL. Optional and default values for URL items create routes that apply to URLs of varying lengths, making it harder to determine what route leads to what action method.

A typical route that follows these rules looks like this:

routes.MapRoute( _
            "GetCustomerSales", _
            "Customer/Sales/{CustId}/{Year}", _
            New With {.controller = "CustomerMaster", .action = "GetSales"}

This route clearly links the URL http://MyServer.com/Customer/Sales/A123/2016 to the CustomerMaster controller's GetSales method while setting the CustId and Year parameter to A123 and 2016, respectively. No doubt, no question.

As an extra benefit to this design, you can now change your controller and action method names without breaking any bookmarks scattered around the Web because they're no longer part of your site's URLs.

You've probably noticed that the default rule that comes out-of-the-box with ASP.NET MVC violates all of these rules. Quite frankly, I think any system that causes you to embed your class names and method names in your UI is a bad system -- not to mention, with its optional and default parameters, the default rule matches to any URL that has one, two or three components.

However, following my rules will cause you to have one route for every controller/action method in your application. I can see that could be overwhelming, especially in a large application. In that case, you have my permission to embed your action method names in your routes. I know that's all that was standing in your way.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

Subscribe on YouTube