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 Global.asax  file, as there's no such thing anymore, nor in the new Startup.cs  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 RouteConfig.cs , WebApiConfig.cs or other intermediate handlers frequently shipped with the previous ASP.NET versions and MVC project templates.

The reason behind the MapRoute  disappearance is related to the fact that there's no need for it anymore. Routing is handled  by the two brand-new   services.AddMvc()  and services.UseMvc()  methods called within the Startup.cs  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 UseMvc()  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 Startup.cs  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 [RouteAttribute]  and/or [HttpGet]  / [HttpPost]  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  UseMvc()  method.

That's it for now: happy coding!

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. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

2 Comments on “Custom Routing and Action Method Names in ASP.NET 5 and ASP.NET MVC 6”

  1. Pingback: How to Add ASP.NET Web API support to your ASP.NET MVC Application

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.