Attribute Routing in ASP.NET MVC 5

Attribute Routing in ASP.NET MVC 5

Ben Daland
Business Analytics Specialist
John Daniel Associates, Inc. 
Ben’s Profile

Introduction

Attribute Routing was introduced with the release of MVC 5 and allows developers to define routes on controller actions and at the controller class level. At its core, Attribute Routing still maintains the same mechanism of the routing framework. With the conventional based routing, the routes are physically separated in a single from the controllers and actions they apply to. This can become messy when a project gets larger and has custom routes, and it often takes more time to understand the relationships. The Attribute Routing feature makes life easier for us developers.

Enabling Attribute Routing

To enable Attribute Routing we need to change the route collection class by calling the MapMvcAttributeRoutes method. Make sure to call this method before the convention-based routing.

Code Sample 1

Basics of Defining a Route

Code Sample 2

In the example above I’ve added a route attribute to the About action which will change the route to {Controller}/Users/About.

Code Sample 3

Say we want to specify a route prefix for actions or the entire controller… To do this we use the [RoutePrefix] attribute. In the example above, I’ve applied it to the controller instead of each action. This changes the routes of all of the actions from Home to Movies. I’ve also set a Route attribute at the controller level and set the default action as a parameter.

Say you want to override the common prefix for one action and not have to set the RoutePrefix for each action… To do that, look at the example below. For the About action I‘ve added a Route and started it with a tilde (~). This is used to override the route prefix.

Code Sample 4

URI Parameters

To specify a parameter, we need to add it to the Route attribute. In the example below, I’ve made an action with an optional parameter and one with a default value. To make a parameter optional in the route, add a question mark (?) to the parameter.

Code Sample 5

Next we want to start adding constraints to our route parameter to restrict how the parameters in the route are matched. In the example above, it searched for a Movie by ID, but now we want to make sure this action is returned when the ID parameter is an integer.

Code Sample 6

The general syntax for imposing constraint is {parameter:constraint}

Below is a list of constraints with examples:

Constraint Description Example
alpha Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) {x:alpha}
bool Matches a Boolean value. {x:bool}
datetime Matches a DateTime value. {x:datetime}
decimal Matches a decimal value. {x:decimal}
double Matches a 64-bit floating-point value. {x:double}
float Matches a 32-bit floating-point value. {x:float}
guid Matches a GUID value. {x:guid}
int Matches a 32-bit integer value. {x:int}
length Matches a string with the specified length or within a specified range of lengths. {x:length(6)}
{x:length(1,20)}
long Matches a 64-bit integer value. {x:long}
max Matches an integer with a maximum value. {x:max(10)}
maxlength Matches a string with a maximum length. {x:maxlength(10)}
min Matches an integer with a minimum value. {x:min(10)}
minlength Matches a string with a minimum length. {x:minlength(10)}
range Matches an integer within a range of values. {x:range(10,50)}
regex Matches a regular expression. {x:regex(^\d{3}-\d{3}-\d{4}$)}

Conclusion

Attribute Routing is a welcomed new feature of MVC 5 that will make the developer’s life easier. I’ve covered some of the basic uses and tricks in this post. Hopefully you find this equally as useful!