在ASP中,RouteLink和ActionLink有什么区别?净MVC吗?

时间:2022-10-10 03:29:34

I think that the title pretty much sums it up:

我认为题目差不多概括了这一点:

What's the difference between RouteLink() and ActionLink() in ASP.NET MVC?

在ASP中,RouteLink()和ActionLink()有什么区别?净MVC吗?

i.e. when do you use Html.RouteLink() and when do you use Html.ActionLink() in your View?

例如,什么时候使用Html.RouteLink(),什么时候在视图中使用Html.ActionLink() ?

4 个解决方案

#1


72  

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,指向通过名称或路径值确定的特定路径。

#2


63  

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()使快速生成ActionLinks变得很容易,并将为您提供对呈现内容的基本控制。如果你没有太多的路线,或者不需要给出太多或太具体的信息,这将会很好地完成工作。

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.

Html.RouteLink()方法采用稍微不同的参数,因此可以对事物的处理方式进行更详细的控制。当我的场景稍微复杂一点,或者有更详细的路由结构时,我倾向于使用这种方法。一个例子是最近的一个项目,我(为了灵活性)宁愿有几个不同的路线,这些路线都非常简单,而不是一个复杂的路线,可以容纳大量的信息。因此,我为同一个控制器设置了4到5条路由,所有的路由都指定了默认操作。我主要使用的是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...).

你想怎么用就怎么用,它们对你的项目有什么意义。这两种方法都没有什么好的/不好的地方(这是其他方法所不能比拟的…)

#3


19  

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要快一些,而且永远不能匹配错误的路由,因为您更改了路由表。

#4


12  

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链接到特定控制器的特定动作。我在我的视图中使用这两个,这取决于我想要的链接类型!

#1


72  

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,指向通过名称或路径值确定的特定路径。

#2


63  

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()使快速生成ActionLinks变得很容易,并将为您提供对呈现内容的基本控制。如果你没有太多的路线,或者不需要给出太多或太具体的信息,这将会很好地完成工作。

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.

Html.RouteLink()方法采用稍微不同的参数,因此可以对事物的处理方式进行更详细的控制。当我的场景稍微复杂一点,或者有更详细的路由结构时,我倾向于使用这种方法。一个例子是最近的一个项目,我(为了灵活性)宁愿有几个不同的路线,这些路线都非常简单,而不是一个复杂的路线,可以容纳大量的信息。因此,我为同一个控制器设置了4到5条路由,所有的路由都指定了默认操作。我主要使用的是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...).

你想怎么用就怎么用,它们对你的项目有什么意义。这两种方法都没有什么好的/不好的地方(这是其他方法所不能比拟的…)

#3


19  

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要快一些,而且永远不能匹配错误的路由,因为您更改了路由表。

#4


12  

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链接到特定控制器的特定动作。我在我的视图中使用这两个,这取决于我想要的链接类型!