Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: ActionLink vs bare url

In ASP.NET MVC I see I have handy HTML helpers that I can use to construct form fields and any number of other little things. But then there's 'ActionLinks'.

Why use an ActionLink instead of just writing the darn url myself in an HTML anchor tag?

In other words, why would I use

<%: Html.ActionLink("Back to List", "QuantityTypes") %>

instead of just using plain ol' HTML and writing:

<a href="/internal/quantitytypes">Back to List</a>

Surely, I must get something extra with the ActionLink. I'm just missing it, right?

like image 203
Dan Esparza Avatar asked Aug 12 '10 04:08

Dan Esparza


1 Answers

The action link will build you the proper URL based on the controller, action, areas, params, etc... It generates the URL based on the URL mapping rules defined in the your MVC routing system. It will map params to the correct url as well depending on if it needs to be included in the URL directly or via a querystring param.

Yes you could do it on your own and just type it all out but it builds the URL for you and ensures the URL that is generate is correct. It's a helper function... it helps you produce valid links :)

You should read Scott Guthrie's post and pay extra attention to the section "Constructing Outgoing URLs from the Routing System". It gives the why and explains other helpers that leverage the routing system.

like image 124
Kelsey Avatar answered Oct 29 '22 07:10

Kelsey