❔ Taghelpers
Microsoft Visual Studio Community 2022 (64-bit) - Current
Version: 17.7.5
I couldn't get taghelpers to work. Do I need to install any library from nuget package in above version for Taghelpers?
4 Replies
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
@using AuthoringTagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, AuthoringTagHelpers
Add this
Add thatvto _ViewImports.cshtml
3rd line is yourPrrojectName(ex. AuthoringTagHelpers)
First could be YourProjectName.OptionalExtraFolderName.Areas.AreaName.Pages
Project Name: TaagHelperMVC
_ViewImports.cshtml
@using AuthoringTagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, TaagHelperMVC.AuthoringTagHelpers
EmailTagHelper.cs
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace AuthoringTagHelpers
{
[HtmlTargetElement("email")]
public class EmailTagHelper : TagHelper
{
private const string EmailDomain = "contoso.com";
// Can be passed via <email mail-to="..." />.
// PascalCase gets translated into kebab-case.
public string MailTo { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "a"; // Replaces <email> with <a> tag
var address = MailTo + "@" + EmailDomain;
output.Attributes.SetAttribute("href", "mailto:" + address);
output.Content.SetContent(address);
}
}
}
Index.cshtml
<address>
One Microsoft Way Copy Version <br />
Redmond, WA 98052-6399<br />
<abbr title="Phone">P:</abbr>
425.555.0100
</address>
<address>
<strong>Support:</strong><email mail-to="Support"></email><br />
<strong>Marketing:</strong><email mail-to="Marketing"></email>
</address>
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.