BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Attribute Based Routing in ASP.NET MVC

| by Jonathan Allen on Nov 12, 2013. Estimated reading time: less than one minute |

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.

Rate this Article

Adoption Stage
Style

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

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and dont miss out on content that matters to you

BT