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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube