ASP.NET Core - Razor Tag Helpers là gì?

ASP.NET Core - Razor Tag Helpers là gì?

ASP.NET Core - Razor Tag Helpers là gì?

Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files. Tag helpers are a new feature and similar to HTML helpers, which help us render HTML.

There are many built-in Tag Helpers for common tasks, such as creating forms, links, loading assets etc. Tag Helpers are authored in C#, and they target HTML elements based on the element name, the attribute name, or the parent tag.

For example, the built-in LabelTagHelper can target the HTML <label> element when the LabelTagHelper attributes are applied.

If you are familiar with HTML Helpers, Tag Helpers reduce the explicit transitions between HTML and C# in Razor views.

In order to use Tag Helpers, we need to install a NuGet library and also add an addTagHelper directive to the view or views that use these tag helpers. Let us right-click on your project in the Solution Explorer and select Manage NuGet Packages....

Manage NuGet Packages
Search for Microsoft.AspNet.Mvc.TagHelpers and click the Install button.

You will receive the following Preview dialog box.

Preview Dialog Box
Click the OK button.

License Acceptance
Click the I Accept button. Once the Microsoft.AspNet.Mvc.TagHelpers is installed, go to the project.json file.

   "version": "1.0.0-*", 
   "compilationOptions": { 
      "emitEntryPoint": true 
   "dependencies": { 
      "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
      "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final", 
      "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 
      "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 
      "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", 
      "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", 
      "EntityFramework.Commands": "7.0.0-rc1-final", 
      "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final" 
   "commands": { 
      "web": "Microsoft.AspNet.Server.Kestrel", 
      "ef": "EntityFramework.Commands" 
   "frameworks": { 
      "dnx451": { }, 
      "dnxcore50": { } 
   "exclude": [ 
   "publishExclude": [ 
In the dependencies section, you will see that "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final" is added.

Now anybody can author a tag helper, so if you can think of a tag helper that you need, you can write your own tag helper.

You can place it right inside your application project, but you need to tell the Razor view engine about the tag helper.

By default, they are not just rendered down to the client, even though these tag helpers look like they blend into the HTML.

Razor will call into some code to process a tag helper; it can remove itself from the HTML and it can also add additional HTML.

There are many wonderful things that you can do with a tag helper, but you need to register your tag helpers with Razor, even the Microsoft tag helpers, in order for Razor to be able to spot these tag helpers in the markup and to be able to call into the code that processes the tag helper.

The directive to do that is addTagHelper, and you can place this into an individual view, or if you plan on using tag helpers throughout the application, you can use addTagHelper inside the ViewImports file as shown below.

@using FirstAppDemo.Controllers 
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers" 
The syntax to register all the tag helpers that are in an assembly is to use asterisk comma (*,) and then the assembly name, Microsoft.AspNet.Mvc.TagHelpers. Because the first piece here is a type name, this is where we could list a specific tag helper if you only wanted to use one.

But if you just wanted to take all the tag helpers that are in this assembly, you can just use the asterisk(*). There are many tag helpers available in the tag helper library. Let us have a look at the Index view.

@model HomePageViewModel  
   ViewBag.Title = "Home"; 


   @foreach (var employee in Model.Employees) { 
            @Html.ActionLink(employee.Id.ToString(), "Details", new { id = employee.Id })
We already have an HTML helper using the ActionLink to generate an anchor tag that will point to a URL that allows us to get to the details of an employee.

Let us first add the Details action in the home controller as shown in the following program.

public IActionResult Details(int id) { 
   var context = new FirstAppDemoDbContext(); 
   SQLEmployeeData sqlData = new SQLEmployeeData(context); 
   var model = sqlData.Get(id); 
   if (model == null) { 
      return RedirectToAction("Index"); 
   return View(model); 

Now we need to add a view for the Details action. Let us create a new view in the Views → Home folder and call it Details.cshtml and add the following code.

@model FirstAppDemo.Models.Employee 
<html xmlns = "

Nguyễn Minh Tuấn

Nguyễn Minh Tuấn

Bài viết liên quan

Trả lời tới ASP.NET Core - Razor Tag Helpers là gì?

Đăng nhập

Cảm ơn bạn đã phản hồi!