ASP MVC 5 Attribute routing VS. Convention-based routing

后端 未结 4 1148
不知归路
不知归路 2021-02-02 13:30

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 lo

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 14:15

    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

提交回复
热议问题