345

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?

Eitan K
  • 837
  • 1
  • 17
  • 39
Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104

6 Answers6

574

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • hmm....So in case there are hundreds of link on a page, then the HTML.ActionLink might lag since it generates the complete HTML for an anchor, right ?? – Pankaj Upadhyay Oct 10 '11 at 05:59
  • 17
    @PankajUpadhyay, you should always use html or url helpers when dealing with urls in an asp.net mvc application. Even if you have hundredths of links, use `Html.ActionLink` to generate them. Don't try to do such micro optimizations. You will end up with ugly code in your views. – Darin Dimitrov Oct 10 '11 at 06:02
  • 2
    dat means I should prefer Html.ActionLink() over Url.Action in all situations when rendering a link is concerned. BTW, then why did Microsoft official tutorial(MVC Music Store) on asp.net website used Url.Action most times whenever a link was needed. – Pankaj Upadhyay Oct 10 '11 at 06:09
  • 7
  • What's the difference between `Html.Action` and `Html.ActionLink`? – Shimmy Weitzhandler Dec 20 '12 at 04:16
  • 5
    @Shimmy, you can read about it here: http://haacked.com/archive/2009/11/17/aspnetmvc2-render-action.aspx – Darin Dimitrov Dec 20 '12 at 06:36
  • 6
    I know this is an old post, but something learned from experience. `Url.Action` is much more performat than `Html.ActionLink`. I had a list of 6,000 items that had 2 `Html.ActionLinks`. It took 6,600ms to render the list. Without the `Html.ActionLinks` it took 52ms. Using `Url.Action` it took 270ms. Granted, 6000 items is a large list, but thought I'd add it for future reference. – Rob Sutherland Feb 19 '18 at 23:05
50

Html.ActionLink generates an <a href=".."></a> tag automatically.

Url.Action generates only an url.

For example:

@Html.ActionLink("link text", "actionName", "controllerName", new { id = "<id>" }, null)

generates:

<a href="/controllerName/actionName/<id>">link text</a>

and

@Url.Action("actionName", "controllerName", new { id = "<id>" }) 

generates:

/controllerName/actionName/<id>

Best plus point which I like is using Url.Action(...)

You are creating anchor tag by your own where you can set your own linked text easily even with some other html tag.

<a href="@Url.Action("actionName", "controllerName", new { id = "<id>" })">

   <img src="<ImageUrl>" style"width:<somewidth>;height:<someheight> />

   @Html.DisplayFor(model => model.<SomeModelField>)
</a>
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Pranav Labhe
  • 1,943
  • 1
  • 19
  • 24
13

@HTML.ActionLink generates a HTML anchor tag. While @Url.Action generates a URL for you. You can easily understand it by;

// 1. <a href="/ControllerName/ActionMethod">Item Definition</a>
@HTML.ActionLink("Item Definition", "ActionMethod", "ControllerName")

// 2. /ControllerName/ActionMethod
@Url.Action("ActionMethod", "ControllerName")

// 3. <a href="/ControllerName/ActionMethod">Item Definition</a>
<a href="@Url.Action("ActionMethod", "ControllerName")"> Item Definition</a>

Both of these approaches are different and it totally depends upon your need.

Arsman Ahmad
  • 2,000
  • 1
  • 26
  • 34
12
<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Company", FormMethod.Get))
{
    <p>
        Find by Name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Search" />
        <input type="button" value="Clear" onclick="location.href='@Url.Action("Index","Company")'"/>
    </p>
}

In the above example you can see that If I specifically need a button to do some action, I have to do it with @Url.Action whereas if I just want a link I will use @Html.ActionLink. The point is when you have to use some element(HTML) with action url is used.

mason
  • 31,774
  • 10
  • 77
  • 121
Rohit Singh
  • 121
  • 1
  • 2
5

You can easily present Html.ActionLink as a button by using the appropriate CSS style. For example:

@Html.ActionLink("Save", "ActionMethod", "Controller", new { @class = "btn btn-primary" })
Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
Altair
  • 71
  • 1
  • 1
  • 6
    This doesn't appear to answer to original question as to what the difference between HTML.ActionLink vs Url.Action is. Perhaps you should use a comment instead of an answer. – Fencer04 Aug 19 '16 at 16:25
  • Your answer doesn't entertaining the original query. – Arsman Ahmad Apr 04 '18 at 06:22
0

I used the code below to create a Button and it worked for me.

<input type="button" value="PDF" onclick="location.href='@Url.Action("Export","tblOrder")'"/>