Html – Are ASP.NET MVC HTML Helpers overrated

asp.net-mvccoding-stylehtmlhtml-helper

It is quite possible that I may not have got the point, but I really can't figure out how ASP.NET MVC's HTML Helpers can help me. Here's a sample: –

HTML:

<a href="ActionName" target="_blank">Click Me</a>

HTML Helper:

<%= Html.ActionLink("Click me", "ActionName", null, new {target="blank"}) %>

My eyes are reading HTML more easily, and it seems counter-intuitive to use the HTML Helpers.

Look at the following arguments:

  • A lot of people (even novices) know how to read HTML. The HTML Helper syntax can confuse easily.
  • In many cases you need to do more typing writing an HTML 'helper', then you need to write the actual HTML.
  • The HTML Helper is going to spew real HTML anyway, so why not write HTML directly?
  • Writing HTML gives you more control over the syntax, and the standards. You can make it conform to whatever HTML standard you want.

Are there any special compelling reasons that I have not understood (since I am self-educated in MVC and there could be gaps) that should make me want to prefer HTML Helpers?

Or are they just code noise?

Best Answer

The primary reason you don't use <a> tags directly is that you don't want to hardcode URLs in your application. The Html.ActionLink method will abstract away the URL generation and you'll specify the controller, action, and other parameters.

So, basically, the two lines you posted in your question are not really equivalent. You should consider adding the dynamic URL generation code to the <a> tag to make them functionally equivalent. Beside that, if you output HTML directly, you'll have to be extremely careful about HTML encoding stuff. Html.ActionLink will do this job for you too.