在ASP.NET Core Tag Helper中,公共属性无法按预期工作

时间:2016-11-12 00:53:22

标签: razor asp.net-core

我已经从ASP.NET核心复制了电子邮件代码助手的演示。

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring

当我在我的视图页面上创建一个如下所示的标记时:

<div>
  <Email MailTo="abc"></Email>
</div>

我希望公共属性MailTo将被设置但不是(当我调试时,它显示为null)。但是,当我从TagHelperOutput获取输出属性时,如下所示:

var mailToWorks = output.Attributes["MailTo"].Value.ToString();

mailToWorks获得了“abc”,正如我所料,但是没有设置公共属性MailTo。我的Tag Helper来源如下。

    [HtmlTargetElement("Email")]
    public class EmailTagHelper : TagHelper
    {
        private const string EmailDomain = "contoso.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)
        {
            var mailToWorks = output.Attributes["MailTo"].Value.ToString();

            output.TagName = "a";    // Replaces <email> with <a> tag

            var address = MailTo + "@" + EmailDomain;
            output.Attributes.SetAttribute("href", "mailto:" + address);
            output.Content.SetContent(address);
        }
    }

1 个答案:

答案 0 :(得分:0)

如果要关闭转小写字母的翻译,可以使用flutter_launcher_icons

HtmlAttributeNameAttribute