Creating custom Tag Helpers
To use all the custom TagHelper
that we will create in this chapter, you need to refer to the current assembly to tell the framework where to find the TagHelper
. Open the _ViewImports.cshtml
file in the View/
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 in 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 from the 90s, 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:using Microsoft.AspNetCore.Razor.TagHelpers; namespace TagHelperSample.TagHelpers; [HtmlTargetElement...