Tag Helpers are a feature in ASP .NET Core that allow you to enable server-side code to participate in creating and rendering HTML elements in Razor files. They make Razor markup more readable and maintainable by providing an HTML-friendly development experience.
Key Concepts of Tag Helpers
- HTML Element Tag: Tag Helpers target HTML elements based on their tag name.
- Attributes: They target specific attributes on HTML elements.
- C# Classes: Tag Helpers are implemented as C# classes that inherit from the
TagHelper
base class. - IntelliSense Support: They provide IntelliSense in the Razor view, making them easy to discover and use.
Built-in Tag Helpers
ASP .NET Core comes with several built-in Tag Helpers, such as:
- Anchor Tag Helper:
<a asp-controller="Home" asp-action="Index">Home</a>
- Form Tag Helper:
<form asp-controller="Home" asp-action="SubmitForm">
- Input Tag Helper:
<input asp-for="Model.PropertyName" />
- Label Tag Helper:
<label asp-for="Model.PropertyName"></label>
Creating Custom Tag Helpers
Create a Custom Tag Helper Class:
csharpusing Microsoft.AspNetCore.Razor.TagHelpers; [HtmlTargetElement("custom-button")] public class CustomButtonTagHelper : TagHelper { public string Text { get; set; } public string Color { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = "button"; // Replaces <custom-button> with <button> output.Attributes.SetAttribute("style", $"background-color: {Color};"); output.Content.SetContent(Text); } }
Register the Tag Helper:
In the
_ViewImports.cshtml
file, register the Tag Helper so it can be used in Razor views.html@addTagHelper *, YourAssemblyName
Use the Custom Tag Helper in a Razor View:
html<!-- Using the custom-button Tag Helper --> <custom-button text="Click Me" color="blue"></custom-button>
Using Built-in Tag Helpers in Razor Pages
Anchor Tag Helper:
Generates an anchor (
<a>
) element with the specified controller and action.html<a asp-controller="Home" asp-action="Index">Home</a>
Form Tag Helper:
Generates a form element with specified action and controller.
html<form asp-controller="Home" asp-action="SubmitForm" method="post"> <input type="text" name="Name" /> <button type="submit">Submit</button> </form>
Input Tag Helper:
Generates an input element based on the model property.
html@model YourNamespace.YourModel <form asp-action="Save"> <div> <label asp-for="Name"></label> <input asp-for="Name" class="form-control" /> </div> <button type="submit">Save</button> </form>
Label Tag Helper:
Generates a label element for the specified model property.
html<label asp-for="Name"></label>
Validation Message Tag Helper:
Displays validation messages for a specified model property.
html<span asp-validation-for="Name" class="text-danger"></span>
Environment Tag Helper:
Renders content based on the current hosting environment.
html<environment include="Development"> <link rel="stylesheet" href="~/css/dev-styles.css" /> </environment> <environment exclude="Development"> <link rel="stylesheet" href="~/css/prod-styles.css" /> </environment>
Advantages of Using Tag Helpers
- HTML-Friendly: They look and feel like standard HTML attributes.
- IntelliSense: Provides auto-completion and validation in Razor views.
- Reusability: Custom Tag Helpers can encapsulate complex logic and be reused across views.
- Separation of Concerns: Helps keep Razor views clean and focused on markup, moving logic to the Tag Helper classes.
Example: Using Tag Helpers in Razor Pages
Model:
csharppublic class Person { public string Name { get; set; } public int Age { get; set; } }
Page Model:
csharppublic class IndexModel : PageModel { [BindProperty] public Person Person { get; set; } public void OnGet() { Person = new Person(); } public IActionResult OnPost() { if (!ModelState.IsValid) { return Page(); } // Process form data here return RedirectToPage("Success"); } }
Razor Page:
html@page @model YourNamespace.Pages.IndexModel <form method="post"> <div> <label asp-for="Person.Name"></label> <input asp-for="Person.Name" class="form-control" /> <span asp-validation-for="Person.Name" class="text-danger"></span> </div> <div> <label asp-for="Person.Age"></label> <input asp-for="Person.Age" class="form-control" /> <span asp-validation-for="Person.Age" class="text-danger"></span> </div> <button type="submit">Submit</button> </form>
In summary, Tag Helpers in ASP .NET Core provide a powerful way to integrate server-side logic with HTML markup, making Razor views cleaner and more maintainable. By leveraging both built-in and custom Tag Helpers, developers can create rich, reusable, and intuitive HTML components.