Custom Routing and Action Method Names in ASP.NET 5 and ASP.NET MVC 6

Custom Routing and Action Method Names in ASP.NET 5 and ASP.NET MVC 6

In case you’re using the (at the time of writing) brand-new ASP.NET 5 with ASP.NET MVC 6 frameworks, you’ll have already witness the fact that the MVC pipeline has been completely rewritten in order to merge the MVC and WebAPI modules into a single, lightweight framework able to handle both worlds. The advantages are many, and I will cover most of them in a series of dedicated post. The major disadvantage is that we need to learn a lot of new stuff, even if we’re MVC and/or WebAPI/WebAPI2 experts, simply because the new approach is quite different from the previous ones.

One of the first thing you’ll notice, which is most likely the reason you’re reading this post, is that the Routes.MapRoute method is gone. You won’t find it in your   file, as there’s no such thing anymore, nor in the new   file which now contains a very small amount of code. And you’ll also notice that there is no ad-hoc configuration file to take care of it such as  ,  or other intermediate handlers frequently shipped with the previous ASP.NET versions and MVC project templates.

The reason behind the   disappearance is related to the fact that there’s no need for it anymore. Routing is handled  by the two brand-new     and   methods called within the   file, which respectively register MVC using the Dependency Injection framework built into ASP.NET 5 and add a set of default routes to our app. We can take a look of what happens behind the hood by looking  at the current implementation of the   method in the framework code (relevant lines in yellow):

The good thing about this is that the framework now handles all the hard work, iterating through all the Controller’s Actions and setting up their default routes, thus saving you some redundant work.

The bad thing is, there’s little or no documentation about how you could add your own routes. Luckily enough, you can easily do that by using either a Convention-Based and/or an Attribute-Based approach, which also goes by the name of Attribute Routing.

Convention-Based Approach

In your   class, replace this:

with this:

Attribute-Based Approach

A great thing about MVC6 is that you can also define routes on a per-controller basis by decorating either the Controller class and/or the Action methods with the appropriate   and/or   /   template parameters, such as the following:

This controller will handle the following requests:

  • [GET] api/items
  • [GET] api/items/5
  • [GET] api/items/GetLatestItems
  • [GET] api/items/GetLatestItems/5
  • [POST] api/items/PostSomething

Also notice that if you use the two approaches togheter, Attribute-based routes (if/when defined) would override Convention-based ones, and both of them would override the default routes defined by the built-in    method.

That’s it for now: happy coding!

RELATED POSTS

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan