HTML.ActionLink vs Url.Action in ASP.NET Razor

Pankaj Upadhyay picture Pankaj Upadhyay · Oct 10, 2011 · Viewed 473.2k times · Source

Is there any difference between HTML.ActionLink vs Url.Action or they are just two ways of doing the same thing?

When should I prefer one over the other?

Answer

Darin Dimitrov picture Darin Dimitrov · Oct 10, 2011

Yes, there is a difference. Html.ActionLink generates an <a href=".."></a> tag whereas Url.Action returns only an url.

For example:

@Html.ActionLink("link text", "someaction", "somecontroller", new { id = "123" }, null)

generates:

<a href="/somecontroller/someaction/123">link text</a>

and Url.Action("someaction", "somecontroller", new { id = "123" }) generates:

/somecontroller/someaction/123

There is also Html.Action which executes a child controller action.