Creating custom TagHelpers
To use all the custom TagHelpers
that we will create in this chapter, you need to refer to the current assembly to tell the framework where to find the TagHelpers
. Open the _ViewImports.cshtml
file in the View/Shared/
folder and add the following line at the end of the file:
@addTagHelper *, TagHelperSample
Here's a quick example showing how to extend an existing tag using a TagHelper
:
- Let's assume we need to have a tag configured as bold and colored in a specific color:
<p strong color="red">Use this area to provide additional information.</p>
This looks like pretty old-fashioned HTML out of the nineties, but this is just to demonstrate a simple
TagHelper
. - The current method to do this task is to use a
TagHelper
to extend any tag that has an attribute calledstrong
, as shown in the following code snippet:[HtmlTargetElement(Attributes = "strong")] public class StrongTagHelper : TagHelper...