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

  • C# Slides in Usage Ranking of Programming Languages

    "The fact that C# lost three places in the ranking of language communities during the last three years is mostly explained by its slower growth compared to C/C++ and PHP."

  • Telerik UI for Blazor Updated

    Progress announced an update to its Telerik UI for Blazor components, targeting Microsoft's open source Blazor framework that lets C# coders create web apps without having to rely upon JavaScript.

  • Infragistics Unveils UI Components for Blazor

    Infragistics, specializing in third-party UI/UX controls and tools, unveiled a new offering targeting Blazor, Microsoft's red-hot open source framework that allows for C#-based web development instead of traditional mainstay JavaScript.

  • AWS Open Sources Tool for Porting .NET Framework Apps to .NET Core

    Leading cloud computing platform Amazon Web Services open sourced the it announced in July for helping users port old .NET Framework applications to the new .NET Core framework.

  • Uno Platform Ports Windows Calculator to Linux

    Uno Platform has ported the famed Windows Calculator, open sourced last year, to Linux as part of a continuing "proof point" effort to demonstrate the reach of what it describes as the sole UI offering available to target Windows, WebAssembly, iOS, macOS, Android and Linux with single-codebase applications coded in C# and XAML.

Upcoming Events