Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC 5 Attribute routing VS. Convention-based routing

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.

like image 239
Jaime Sangcap Avatar asked Jan 22 '14 06:01

Jaime Sangcap


People also ask

What is the difference between convention based and attribute based routing?

In short, Convention Routing approaches Routing from the general case; you generally add some routes that will match all or most of your URLs, then add more specific routes for more specialized cases. The other way to approach this problem is via Attribute Routing.

What is the advantage of attribute routing over conventional routing?

Here are a few advantages of attribute based routing, Helps developer in the debugging / troubleshooting mode by providing information about routes. Reduces the chances for errors, if a route is modified incorrectly in RouteConfig. cs then it may affect the entire application's routing.

What is convention based routing in MVC?

ASP.NET Core Convention based routing is typically used with the MVC controllers ( i.e. Controller with views). It configures set of Endpoints based on conventions of URL Patterns. We configure these conventions in the UseEndpoints method in the Configure method of startup class.


1 Answers

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/

like image 154
louis.schilling Avatar answered Oct 01 '22 12:10

louis.schilling