ASP MVC 5属性的路由VS.根据公约的路由 [英] ASP MVC 5 Attribute routing VS. Convention-based routing

查看:136
本文介绍了ASP MVC 5属性的路由VS.根据公约的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP MVC 5具有一个名为属性路由新的路线。我看到它的方式,路线,现在散落每个控制器上不同于公约为基础的,有一个位置RouteConfig.cs这里你可以查看你的路线,也作为某种方式对你的站点的路由文件。

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.

推荐答案

要解决第一个问题,分散路由具有许多优点:

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


  1. 它把相邻对实现该路由控制器动作的路由信息​​。这有助于调试和故障排除,以及提供给您的解决方案的路由信息​​快速搜索的能力。

  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.

这降低了更改路线的过程的风险。在RouteConfig.cs或WebApiConfig.cs(在Web API的解决方案的情况下),可能存在无意中改变路线错误或其他不利影响应用程序的其他部分。

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.

您可能还需要包括可接受的HTTP方法,允许用户类型和注册的优先级,而如果包含在基于属性的路线,把所有这些信息汇集于一处。

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.

该职位提供​​了灵感和加强我上文所述,并进入更详细:<一href=\"http://kevinmontrose.com/2011/07/25/why-i-love-attribute-based-routing/\">http://kevinmontrose.com/2011/07/25/why-i-love-attribute-based-routing/

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/

这篇关于ASP MVC 5属性的路由VS.根据公约的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆