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

  • Visual Studio 2019 v16.7 Ships with Better Git Integration

    Better GitHub integration and other improvements highlight the new Visual Studio 2019 Version 16.7 and first preview of v16.8.

  • Visual Studio Codespaces Private Preview Woos C++ Developers

    Microsoft announced a private preview of Visual Studio Codespaces, which eases the setup and use of cloud-powered development environments that can be used from anywhere for remote development and other scenarios, targeting C++ console app and library developers.

  • Data Prep for Machine Learning: Normalization

    Dr. James McCaffrey of Microsoft Research uses a full code sample and screenshots to show how to programmatically normalize numeric data for use in a machine learning system such as a deep neural network classifier or clustering algorithm.

  • Microsoft Intros Azure Well-Architected Framework Best Practices

    Taking a page from the Amazon Web Services (AWS) book on cloud computing platforms, Microsoft has introduced its own Azure Well-Architected Framework, providing a set of architecture best practices to help users build and deliver great solutions and improve the quality of cloud workloads.

  • Creating a Progressive Web App with Blazor WebAssembly

    Not surprisingly, it's dead easy to create an app in Blazor that runs outside of the browser window and (potentially) in an offline mode. Before you get carried away, though, there are some key design decisions to make.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events