【问题标题】:TagHelpers doesn't get invoked after moving them to a different projectTagHelpers 在将它们移动到不同的项目后不会被调用
【发布时间】:2017-12-07 09:13:24
【问题描述】:

我在 asp.net core 找不到和调用我的标签助手时遇到了一些问题。在我将 taghelpers 从单独的程序集 (LC.Tools.Utility.TagHelpers) 移动到 LC.Tools.Utility 之后,即使命名空间是和以前一样。

示例 cshtml

<lc:css src="styles.min.css" />
<lc:css src="styles.bootstrap.min.css" />
<lc:css src="styles._layout.min.css" />

查看页面来源时,标签完全相同。

_ViewImports.cshtml

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers 
@addTagHelper *, LC.Smokers.Albino.TagHelpers
@addTagHelper *, LC.Tools.Utility.TagHelpers

LC.Tools.Utility.TagHelpers.IncludeCssTagHelper.cs在程序集中 LC.Tools.Utility):

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace LC.Tools.Utility.TagHelpers
{
    [HtmlTargetElement("lc:css", Attributes = "src", TagStructure = TagStructure.WithoutEndTag)]
    public class IncludeCssTagHelper : Base
    {
        public IncludeCssTagHelper(IHostingEnvironment env) : base(env) { }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "link";
            output.TagMode = TagMode.SelfClosing;

            bool isDebug = this.Environment.IsDevelopment() && this.DebugSrc.Length > 0;
            string path = "/CSS/";

            if (isDebug)
            {
                path += this.DebugSrc;

                if (Utility.IsUrl(this.DebugSrc))
                {
                    path = this.DebugSrc;
                }
            }
            else if (!string.IsNullOrEmpty(this.Src))
            {
                path += this.Src;

                if (Utility.IsUrl(this.Src))
                {
                    path = this.Src;
                }
            }

            output.Attributes.Clear();

            output.Attributes.SetAttribute("href", path.ToLower());
            output.Attributes.SetAttribute("rel", "stylesheet");
            output.Attributes.SetAttribute("type", "text/css");

            base.Process(context, output);
        }

        [HtmlAttributeName("src")]
        public string Src { get; set; }

        [HtmlAttributeName("debug-src")]
        public string DebugSrc { get; set; } = string.Empty;
    }
}

我已经尝试从所有项目中删除 bin 和 obj 文件夹,并且 重建整个解决方案并删除并添加对 项目。

有什么想法吗?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core asp.net-core-mvc


    【解决方案1】:

    addTagHelper 指令需要两个逗号分隔的参数。 docs 有这样说它是如何工作的:

    @addTagHelper 之后的第一个参数指定要加载的 Tag Helpers(我们对所有 Tag Helpers 使用“*”),第二个参数“Microsoft.AspNetCore.Mvc.TagHelpers”指定程序集 包含标签助手。

    注意我用粗体突出显示的内容:这不是需要的命名空间;这是大会。在您的示例中,您需要更改第二个参数以反映新的托管程序集:

    @addTagHelper *, LC.Tools.Utility
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多