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

Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

2 comments:

Stuart Ben said...

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

Anonymous said...

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