Displaying URL’s in ASP.NET MVC 3 - Difference between Html.ActionLink vs Url.Action

The Html.ActionLink method renders a hyperlink (<a href=".."></a>) to another controller action. The Url.Action URL helper works the same way as Html.ActionLink, but it does not return an anchor tag.

Let’s see an example. Add the following code to your Index.cshtml or any view you want to display this on

html-actionlink-urlaction-mvc3

Run the page and observe the source generated

html-actionlink-urlaction-source

While Html.ActionLink generates an anchor tag, Url.Action on the other hand generates just the URL. Generating just the URL could be helpful in scenarios where you want a convenient way to generate HTML for a link manually.

Note: For security purpose, make sure to encode your URL using Url.Encode or Html.AttributeEncode:

OUTPUT

urlaction-mvc3

2 comments:

  1. Thanks for sharing this! I wish there would be more info about that:)

    ReplyDelete
  2. Hey is there any way to get this to work in a chrome browser?

    ReplyDelete