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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube