views:

48

answers:

2

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 damn 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?

+5  A: 

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.

Kelsey
+1 for the ScottGu article -- good reading.
Dan Esparza
A: 

You get centralized control of your URL's. So next time you need to change one for SEO purposes, you don't have to go searching for every place in the application, just switch it in the Global.asax.

Yuriy Faktorovich
Interesting. Do you have an example of what this would look like?
Dan Esparza