ASP.NET Core 2.1 - Custom TagHelpers

Tag Helpers are classes written in C# but are attached to HTML elements to execute server-side code from View Razor. A tag helper enables us to run the server-side code to generate HTML, CSS, Javascript code in Razor View.

To create a custom tag helper, the first step is to create a class that inherits from "TagHelper" class. This class has a virtual method to generate HTML tags. It contains both synchronous (Process) and asynchronous (ProcessAsync) implementation of the virtual method.

  1. public virtual Task ProcessAsync(TagHelperContext context, TagHelperOutput output);  
  2. public virtual void Process(TagHelperContext context, TagHelperOutput output);  

In other words, View created in HTML has its presentation logic defined in C #, which runs on the server.

So how are we going to use Tag Helpers?

To use internal or custom Tag Helpers, you must first reference the TagHelper library named Microsoft.AspNetCore.Mvc.TagHelpers. It can also be via Nuget. Once referenced, import it using the @AddTagHelper directive as shown below within _ViewImports.cshtml

STEP1 - Creating the ASP .NET Core Project in VS 2017

Open VS 2017 and on the File menu click New Project. Then select the Visual C # -> Web template and check ASP .NET Core Web Application (.NET Core). 

Creating the ASP .NET Core Project in VS 2017

Creating the ASP .NET Core Project in VS 2017


STEP2 - Creating a new class to define our CustomTagHelper

Create a new class named CustomTagHelper and add the code as shown below:

@addTagHelper *, CustomTagHelper



STEP3 - Import our Tag Helper Class

Then import the newly created Tag Helper. This can be done by adding a line in the _ViewImports.cshtml file as shown below,


  1. using Microsoft.AspNetCore.Razor.TagHelpers;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. namespace aspnet_core {  
  8.     [HtmlTargetElement("first-tag-helper")]  
  9.     public class CustomTagHelper: TagHelper {  
  10.         public string Name {  
  11.             get;  
  12.             set;  
  13.         }  
  14.         public override void Process(TagHelperContext context, TagHelperOutput output) {  
  15.             output.TagName = "CustomTagHelper";  
  16.             output.TagMode = TagMode.StartTagAndEndTag;  
  17.             var sb = new StringBuilder();  
  18.             sb.AppendFormat("<span>Hi! {0}</span>"this.Name);  
  19.             output.PreContent.SetHtmlContent(sb.ToString());  
  20.         }  
  21.     }  
  22. }  

STEP4 - Update view

The last is to update the respective view and the code for this is,

Update view


STEP5 - Result

We can also pass the model data to the tag helper via model binding by creating properties of type "ModelExpression". Using HtmlAttributeName attribute, we can create a friendly attribute name.

Executing the project, we will obtain the following result,