BT

Attribute Based Routing in ASP.NET MVC

by Jonathan Allen on Nov 12, 2013 |

In ASP.NET MVC 4 and earlier routing was handled exclusively via adding paths to a RouteCollection. Though is starts with simple convention based routing, as a website grows and the special cases accumulate it can become quite complex. And since the routes are physically separated from the controllers they apply to, it can take some detective work to understand the relationships.

With the introduction of ASP.NET MVC 5, Microsoft hopes to simplify the experience using attribute-based routing. Using the same basic pattern syntax as the imperative model, a declarative Route attribute can be applied to controller methods.

To specify a default route for a given controller, simply add the Route attribute to the class with the path “{action=method}” where “method” is the name of the default method. You can also apply a RoutePrefix attribute to a controller, which reduces the length of the paths contained by the controller. Area registration can also be eliminated using attributes, in this case the RouteArea attribute.

These routes support constraints, optional URI parameters, and default values. MVC constraints follow the same rules as Web API’s attribute based routing.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT