我在ASP.net Core 2.0中的项目。我正在尝试创建自定义标记帮助程序,但没有成功。
例如,我在下面创建的Tag帮助器未显示在intellisense中或在contact.html中呈现。
帮助?
EmailTagHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Threading.Tasks;
namespace Tallog.TagHelpers
{
[HtmlTargetElement("my-email")]
public class EmailTagHelper : TagHelper
{
private const string EmailDomain = "example.com";
// Can be passed via <email mail-to="..." />.
// Pascal case gets translated into lower-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);
}
}
}
_ViewImports.cshtml
@using Microsoft.AspNetCore.Identity
@using Tallog
@using Tallog.Models
@using Tallog.TagHelpers
@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"
@addTagHelper "*, Tallog.TagHelpers"
Contact.html
@{
ViewData["Title"] = "Contact";
ViewData["PageTitle"] = "Contact";
}
<h3>@ViewData["Message"]</h3>
<address>
<strong>Support:</strong><my-email mail-to="Support"></my-email><br />
<strong>Marketing:</strong><my-email mail-to="Marketing"></my-email>
</address>