ASP.NET MVC 中的 RouteLink 和 ActionLink 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/864827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between RouteLink and ActionLink in ASP.NET MVC?
提问by Guy
I think that the title pretty much sums it up:
我认为标题几乎总结了它:
What's the difference between RouteLink()and ActionLink()in ASP.NET MVC?
ASP.NET MVCRouteLink()和ActionLink()ASP.NET MVC 有什么区别?
i.e. when do you use Html.RouteLink()and when do you use Html.ActionLink()in your View?
即你什么时候使用Html.RouteLink(),什么时候Html.ActionLink()在你的视图中使用?
采纳答案by Chad Moran
Action and Routes don't have to have a 1:1 relationship.
Action 和 Routes 不一定是 1:1 的关系。
ActionLink will generate the URL to get to an action using the first matching route by action name.
ActionLink 将生成 URL 以使用按操作名称匹配的第一个路由来访问操作。
RouteLink will generate a URL to a specific route determined either by name or route values.
RouteLink 将生成一个 URL,指向由名称或路由值确定的特定路由。
回答by Tomas Aschan
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.
Html.ActionLink()可以轻松快速地生成 ActionLink,并且可以让您基本控制呈现的内容。如果您没有太多路线,或者不需要提供太多或太具体的信息,这将很好地完成工作。
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 RouteLinkversion, because when I specified a route name, the default parameters were entered automatically.
该Html.RouteLink()方法采用稍微不同的参数,因此可以让您更详细地控制事物的处理方式。当我的场景有点复杂,或者当我有更详细的路线结构时,我倾向于使用这种方法。
一个例子是最近的一个项目,我(为了灵活性)宁愿有几条不同的路线,这些路线都非常简单,而不是一条允许大量信息的复杂路线。因此,我最终为同一个控制器创建了四到五个路由,所有路由都指定了默认操作。我用的大多是RouteLink版本,因为当我指定路由名称时,默认参数是自动输入的。
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...).
根据您的感觉使用它们,并且它们对您的项目有意义。它们中的任何一个都没有任何优点/缺点(与其他一些不匹配......)。
回答by Craig Stuntz
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 的速度要快一些,并且永远不会匹配错误的路由,因为您更改了路由表。
回答by Simon Steele
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!
RouteLink 使用路由的名称,因此如果您的路由名称可靠且相当独特,那么即使要使用的操作名称发生变化,这也是相同的。ActionLink 链接到特定控制器的特定操作。我在我的观点中同时使用两者,这取决于我所追求的链接类型!

