ASP.NET MVC 中的 RouteLink 和 ActionLink 有什么区别?
我认为标题几乎概括了这一点:
What's the Difference between RouteLink()
and ActionLink()
in ASP.NET MVC?
即什么时候在视图中使用 Html.RouteLink()
以及什么时候使用 Html.ActionLink()
?
I think that the title pretty much sums it up:
What's the difference between RouteLink()
and ActionLink()
in ASP.NET MVC?
i.e. when do you use Html.RouteLink()
and when do you use Html.ActionLink()
in your View?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
操作和路由不必具有 1:1 的关系。
ActionLink 将生成 URL 以使用按操作名称匹配的第一个路由来访问操作。
RouteLink 将生成由名称或路由值确定的特定路由的 URL。
Action and Routes don't have to have a 1:1 relationship.
ActionLink will generate the URL to get to an action using the first matching route by action name.
RouteLink will generate a URL to a specific route determined either by name or route values.
实际上,这两种方法的输出是相同的,但生成方式略有不同:
Html.ActionLink()
使您可以轻松快速地生成 ActionLink,并且可以让您对呈现的内容进行基本控制。 如果您没有太多路线,或者不需要提供太多或太具体的信息,那么这将很好地完成工作。Html.RouteLink()
方法采用略有不同的参数,从而使您可以更详细地控制事物的处理方式。 当我的场景稍微复杂一点,或者当我有更详细的路线结构时,我倾向于使用这种方法。一个例子是最近的一个项目,我(为了灵活性)宁愿有几条不同的路线,这些路线都非常简单,而不是一条允许大量信息的复杂路线。 因此,我最终为同一个控制器提供了四到五个路由,所有路由都指定了默认操作。 我主要使用
RouteLink
版本,因为当我指定路由名称时,会自动输入默认参数。根据您的喜好以及对您的项目有意义的方式使用它们。 它们中的任何一个实际上都没有优点/缺点(与其他一些不匹配......)。
Actually, the output from the two methods is the same, but it is generated in slightly different ways:
Html.ActionLink()
makes it easy to generate ActionLinks fast, and will give you basic control over what is rendered. If you don't have too many routes, or don't need to give too much or too specific information, this will do the work just fine.The
Html.RouteLink()
method takes slightly different arguments, and thus gives you a little more detailed control over the way things are handled. I tend to use this method when my scenario is a little more complicated, or when I have a more detailed route structure.One example is a recent project where I (for flexibility) rather had several different routes, which were all quite simple, than one complex one that would allow for a lot of information. Thus, I ended up with four or five routes for the same Controller, all with a default action specified. I mostly used the
RouteLink
version, because when I specified a route name, the default parameters were entered automatically.Use them as you feel like, and as they make sense to your project. There is really no upside/downside to either of them (that is not matched by some other...).
除了此处给出的其他答案之外,RouteLink 的速度要快一些,并且永远不会因为您更改了路由表而匹配错误的路由。
In addition to the other answers given here, RouteLink is a little bit faster, and cannot ever match the wrong route because you changed your routing table.
RouteLink 采用路由名称,因此如果您的路由名称可靠且相当唯一,那么即使要使用的操作名称发生更改,路由名称也将是相同的。 ActionLink 改为链接到特定控制器的特定操作。 我在我的观点中使用这两种方法,具体取决于我想要什么样的链接!
RouteLink takes the name of a route, so if your route names are reliable and fairly unique then this will be the same even if the action name to be used changes. ActionLink links to a specific action of a specific controller instead. I use both in my views, depending on what kind of link I'm after!