Tag Helpers In MVC 6

Introduction

Tag helpers is a new feature introduced in MVC 6 to improve readability of razor code. Tag helpers function similarly to HTML helpers, by translating the custom tags (defined in our tag helper) into HTML code.

Why do we need tag helpers

Well, the first question that comes to our mind is, why do we even need tag helpers? Or for that reason, why do we need a HTML helper. The main reason we need them is to avoid repeating the same snippets of HTML again and again in our project. It enables consistency of code in a project with a large number of developers. Also, it has uses in generating input fields, generating anchor links taking advantage of routing etc.

Tag helpers over HTML helpers

The next question that pops up is why do we even need tag helpers, when HTML helpers can very well do the same job? That is true, we can achieve almost everything that tag helpers can provide us with HTML helpers. But, if you look at a view file created largely using HTML helpers, it will start appearing more like a C# code page than like a HTML page.

For many who love HTML, this doesn’t seem right. People want a HTML page (a view in our case) to look like a HTML page. So enter tag helpers! Tag helpers can help you achieve you do this.

Of course, please note there is nothing stopping you from using HTML helpers to create your view. It is just a choice provided to you by MVC 6 to make your view look much cleaner.

Ok, enough said, let us now look into an example of how to use tag helpers.

Use case: Create a <section> with a <h2> and a <p>

  1. Create a new Visual Studio 2015 ASP.NET 5 MVC project (you can download Visual Studio 2015 community edition for free.

  2. Create a class file where you will implement your custom tag helper.
    1. using Microsoft.AspNet.Razor.TagHelpers;  
    2. using TagHelpersDemo.Models;  
    3.   
    4. namespace TagHelpersDemo.TagHelpers {  
    5.     public class CustomSectionTagHelper: TagHelper {  
    6.         public SectionInfo Info {  
    7.             get;  
    8.             set;  
    9.         }  
    10.   
    11.         public override void Process(TagHelperContext context, TagHelperOutput output) {  
    12.             output.TagName = "section";  
    13.   
    14.             output.Content.SetHtmlContent(  
    15.                 $ @ "<h2> {Info.Heading} </h2>  <  
    16.                 p > {  
    17.                     Info.Content  
    18.                 } < /p>"  
    19.             );  
    20.   
    21.             output.TagMode = TagMode.StartTagAndEndTag;  
    22.         }  
    23.     }  
    24. }  
    SectionInfo Class
    1. namespace TagHelpersDemo.Models {  
    2.     public class SectionInfo {  
    3.         public string Heading {  
    4.             get;  
    5.             set;  
    6.         }  
    7.         public string Content {  
    8.             get;  
    9.             set;  
    10.         }  
    11.     }  
    12. }  
    In the above code, the name of the custom tag helper will be custom-section. Basically, the content of the class minus the TagHelper part, converted to lower-kebab-case.

    You write your logic on how you want your HTML markup rendered in the Process method. The output parameter needs to be set with the way the rendered HTML needs to look like. In our case, we want our custom tag to be translated into a heading and a paragraph inside a section tag.

    For passing the values of the heading and the paragraph, we are using a custom class named, SectionInfo. The Info property in the tag helper class will hope the value of the SectionInfo that we pass from our view file.

  3. Register your tag helper assembly and tag helpers in your _ViewImports file,
    1. @using TagHelpersDemo  
    2. @using TagHelpersDemo.Models  
    3. @using TagHelpersDemo.ViewModels.Account  
    4. @using TagHelpersDemo.ViewModels.Manage  
    5. @using Microsoft.AspNet.Identity  
    6. @inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration  
    7. @addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"  
    8. @addTagHelper "*, TagHelpersDemo"  
    The * indicates that you are registering all your custom tag helpers in your assembly, TagHelpersDemo

  4. Use the tag helper in your view file,
    1. @ {  
    2.     ViewData["Title"] = "About";  
    3. }  
    4.   
    5. <  
    6. custom - section info = "new SectionInfo {  
    7.     Heading = Model.Heading,  
    8.         Content = Model.Content  
    9. }  
    10. " />  
    Output



Well, that is an example of creating and using a tag helper.

Up Next
    Ebook Download
    View all
    Learn
    View all