18

ASP MVC 5 has a new Routing called attribute routing. The way I see it, the routes are now scattered on every controller unlike with the convention-based that there is single location RouteConfig.cs where you can check your routes, which also serves as documentation of your sites routes in some way.

My question is it better to use Attribute routing over the convention-based routing in terms of readability and maintainability? And can someone suggest how to design routes for better maintainability and readibility.

Jaime Sangcap
  • 2,515
  • 6
  • 27
  • 45

4 Answers4

21

To address your first question, scattering the routes has a number of advantages:

  1. It puts the route information adjacent to the controller action that implements that route. This helps in debugging and troubleshooting, as well as providing an ability to quickly search for route information in your solution.

  2. It reduces risk in the process of making changes to routes. In RouteConfig.cs or WebApiConfig.cs (in the case of Web API solutions), the possibility exists to inadvertently change the wrong route or otherwise adversely affect other parts of your application.

  3. You may also want to include acceptable HTTP methods, permitted user types and registration priorities, which if included with the attribute-based routes, put all of that information together in one place.

This post provided inspiration and reinforcement for me on the above, and goes into more detail: http://kevinmontrose.com/2011/07/25/why-i-love-attribute-based-routing/

louis.schilling
  • 211
  • 1
  • 7
2

You can Unit Test your'e routes when using conventional routing, and you also have a "seperation of concerns".

That cant be said about Attribute Routing.

For a large project I would go with conventional, for a small project attribute routing is more than fine.

Derek
  • 8,300
  • 12
  • 56
  • 88
  • 1
    how about for friendly urls? is it ok to add route on each controller action to gain better control of the url? basically its just the same as attribute routing where I create one route on every action, the only difference is they are on a single location, or should I say Routes folder if you really have tons of routes. – Jaime Sangcap Sep 15 '15 at 06:55
  • Im not sure i understand? – Derek Sep 15 '15 at 08:38
0

Attribute routing give you more ad easy options for routing.

Check out following: Routing explanations

Jelle Oosterbosch
  • 1,731
  • 15
  • 17
  • thats the article I've read, yeah attribute routing gives you less verbose routing. but I dont mind doing more keystrokes if it will lead to more maintainable code. I want to know if there is another way to implement those attributes or how is it better than the convention-based. – Jaime Sangcap Jan 23 '14 at 04:50
0

I think from a maintaibnability and readability perspective, if you have a project that fits well with convention based routing then go with that. It's a lot simper to have a few routes in your config file than to decorate every controller.

BUT ...

In my experience convention based routing is too simplistic and often breaks down on projects that are large or more complex. What often happens is that you start with the default {controller}/{action}/{id} route. But then you decide you need some more routes because you need a deeper hierarchy so start adding routes. Something like this: /company/5/employee/7/edit. Then you have to be careful about what order you put your routes in so that the correct route is found. Once you start adding custom routes you might find that more that one route matches a specific request, so you add some route constraints. As your project gets larger and/or more complicated your route config grows in size and complexity making it error prone and difficult to maintain.

Attribute routing gives you more control over your routes because you can map specifc controllers and actions to specific routes and not worry that the wrong route will be matched. Further, since routes are in close proximity to controllers troubleshooting routes is much easier.

TL;DR: At a certain point routes become more difficult to maintain reardless of whether you use convention based routing or attribute routing. Attribute routing is all about having control over your routes and avoiding routing errors.

Another alternative that looks promising is MvcCodeRouting which is namespace based routing

Louise Eggleton
  • 969
  • 2
  • 15
  • 27