2 minute read

3 min read 684 words

1. Introduction

Tag Helpers are a powerful feature in ASP.NET Core MVC that enable server-side code to participate in creating and rendering HTML elements in Razor files. Unlike HTML Helpers (like @Html.TextBoxFor), Tag Helpers look and feel like standard HTML, making them easier to read and maintain for both developers and designers.

With the release of .NET 10, Tag Helpers continue to be the preferred way to build dynamic views, offering better performance and deeper integration with the ASP.NET Core ecosystem.


2. What are Tag Helpers?

Tag Helpers are classes that transform HTML elements in your Razor views. They are activated by the asp- attribute prefix. When the view is rendered, the server processes these attributes and generates the final HTML sent to the browser.

To use them, you must ensure they are opted-in in your _ViewImports.cshtml:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

3. Common and Useful Tag Helpers

A. Anchor Tag Helper (asp-controller, asp-action)

The Anchor Tag Helper enhances the standard <a> tag to generate URLs based on your application’s routes.

Example:

<a asp-controller="Products" asp-action="Details" asp-route-id="5">View Product</a>

Generated HTML:

<a href="/Products/Details/5">View Product</a>

B. Form Tag Helper (asp-action, asp-controller, asp-antiforgery)

Automatically generates the action attribute and includes a hidden Request Verification Token to prevent CSRF attacks.

Example:

<form asp-controller="Account" asp-action="Login" method="post">
    <!-- Form content -->
</form>

C. Input Tag Helper (asp-for)

Binds an <input> element to a model expression. It automatically sets the type, id, name, and adds data-val attributes for client-side validation.

Example:

<input asp-for="Email" class="form-control" />

If Email is a string with [EmailAddress] attribute, it generates:

<input type="email" id="Email" name="Email" value="" class="form-control" data-val="true" ... />

D. Label Tag Helper (asp-for)

Generates the caption and for attribute for a model property.

Example:

<label asp-for="Email"></label>

Generated HTML:

<label for="Email">Email Address</label>

E. Select Tag Helper (asp-for, asp-items)

Easily creates dropdown lists from a collection (like IEnumerable<SelectListItem>).

Example:

<select asp-for="CategoryId" asp-items="Model.Categories">
    <option value="">-- Select Category --</option>
</select>

F. Validation Tag Helpers (asp-validation-for, asp-validation-summary)

Displays validation messages for specific properties or a summary for the whole model.

Example:

<span asp-validation-for="Email" class="text-danger"></span>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>

4. Why Use Tag Helpers in .NET 10?

  1. Readability: They look like standard HTML, reducing the “code noise” in your views.
  2. IntelliSense Support: Visual Studio and JetBrains Rider provide excellent tooling for asp- attributes.
  3. Modern Standards: .NET 10 continues to optimize the rendering pipeline, making Tag Helpers faster than ever.
  4. Extensibility: You can easily create your own custom Tag Helpers for reusable UI components.

5. Conclusion

Tag Helpers are essential for any modern ASP.NET Core developer. They bridge the gap between HTML and C#, providing a clean and intuitive way to build dynamic forms and navigation.

Start replacing your old @Html helpers with Tag Helpers today to make your Razor views cleaner and more maintainable!


Leave a comment