What's the difference between RouteLink and ActionLink in ASP.NET MVC?
Asked Answered
S

4

122

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?

Schoenburg answered 14/5, 2009 at 18:3 Comment(0)
C
79

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.

Cockup answered 14/5, 2009 at 18:11 Comment(2)
+1 for a great blog post. Really handy. And shows there's just too many possibilities. MS guys should only implement the fastest. But people rather use the fancy lambda slowest one.Examination
hey thanks But I think the link is broken didnt reached. pls will you again provide me the link thanksPetes
P
66

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...).

Passage answered 14/5, 2009 at 18:37 Comment(0)
E
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.

Enrich answered 14/5, 2009 at 18:45 Comment(1)
Simone Chiaretta did some performance analysis here: codeclimber.net.nz/archive/2009/04/17/…Harangue
U
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!

Unyielding answered 14/5, 2009 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.