1

In ASP.NET Razor views, I have seen links created in two different ways:

@Html.ActionLink("Download", "Download", new { id = item.Id })

and

<a asp-area="" asp-controller="MyController" asp-action="Download" asp-route-id="@item.Id">Download</a>

Does one method have advantages over the other? The @Html.ActionLink() syntax seems more compact, but perhaps doesn't allow for as much control over the final <a/> tag? Thanks!

1 Answer 1

5

The first one is HTML helper method which exist in MVC4 and 5. The second one is called Tag helpers which appeared in asp.net core. It looks more like pure HTML syntax so even UI designers who are not so familiar with C# can use those to create links.

The link tag helper can do whatever the ActionLink helper can do, and even more. For example, with the link tag helper you can create HTML markup like this.

<a asp-action="Search" asp-controller="Home">
    <i class="glyphicon glyphicon-search"></i>
</a>

You cannot generate the above kind of markup (other element markup inside the anchor tag with ActionLink helper.

Keep in mind that, you can use the HTML helper methods inside asp.net core views. But tag helpers are more easy to read from an HTML perspective.

Compare the code to render the SELECT element in tag helper approach

<select asp-for="UserId" asp-items="@Model.UserList">
   <option>Select one</select>
</select>

vs

@Html.DropDownListFor(a=>a.UserId, Model.UserList,"select one")

IMHO, The tag helper approach is more designer friendly and readable

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.