简体   繁体   English

ASP.NET MVC中的RouteLink和ActionLink有什么区别?

[英]What's the difference between RouteLink and ActionLink in ASP.NET MVC?

I think that the title pretty much sums it up: 我认为标题几乎总结了:

What's the difference between RouteLink() and ActionLink() in ASP.NET MVC? ASP.NET MVC中的RouteLink()ActionLink()什么区别?

ie when do you use Html.RouteLink() and when do you use Html.ActionLink() in your View? 即你何时使用Html.RouteLink() ,何时在你的视图中使用Html.ActionLink()

Action and Routes don't have to have a 1:1 relationship. 动作和路线不必具有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。

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. Html.ActionLink()可以快速生成ActionLinks,并且可以基本控制渲染的内容。 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. Html.RouteLink()方法采用稍微不同的参数,因此可以更加详细地控制事物的处理方式。 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. 我主要使用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...). 他们中的任何一方都没有上行/下行(其他一些都不匹配......)。

除了这里给出的其他答案之外,RouteLink更快一点,并且因为您更改了路由表而无法匹配错误的路由。

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. RouteLink采用路由的名称,因此如果您的路由名称可靠且相当独特,那么即使要使用的操作名称发生更改,这也是相同的。 ActionLink links to a specific action of a specific controller instead. ActionLink链接到特定控制器的特定操作。 I use both in my views, depending on what kind of link I'm after! 我在我的视图中使用两者,取决于我之后的链接类型!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM