Tag helper is not processed in ASP.NET Core 2

I added the following tag helper:

using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace X.TagHelpers
{
    [HtmlTargetElement(Attributes = ValidationForAttributeName + "," + ValidationErrorClassName)]
    public class ValidationClassTagHelper : TagHelper
    {
        private const string ValidationForAttributeName = "k-validation-for";
        private const string ValidationErrorClassName = "k-error-class";

        [HtmlAttributeName(ValidationForAttributeName)]
        public ModelExpression For { get; set; }

        [HtmlAttributeName(ValidationErrorClassName)]
        public string ValidationErrorClass { get; set; }

        [HtmlAttributeNotBound]
        [ViewContext]
        public ViewContext ViewContext { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            Console.WriteLine("\n\n------------!!!!!!---------\n\n");
            ModelStateEntry entry;
            ViewContext.ViewData.ModelState.TryGetValue(For.Name, out entry);
            if (entry == null || !entry.Errors.Any()) return;
            var tagBuilder = new TagBuilder(context.TagName);
            tagBuilder.AddCssClass(ValidationErrorClass);
            output.MergeAttributes(tagBuilder);
        }
    }
}

and then in _ViewImports.cshtmli added the line:

@addTagHelper *, X.TagHelpers

The file is compiled correctly, and if I introduce a syntax error, it dotnet buildwarns me about this.

Then on one of my pages I add:

<div k-validation-for="OldPassword" k-error-class="has-danger"></div>

If I load the page, I see console output on the server side as well k-validation-for, and k-error-classsent to the generated page as is (as opposed to adding a class has-dangerto an attribute class).

What am I doing wrong?

+4
source share
1 answer

Registering tag help requires an assembly, not a namespace, in docs .

... "Microsoft.AspNetCore.Mvc.TagHelpers" , . Microsoft.AspNetCore.Mvc.TagHelpers - ASP.NET.

, :

@addTagHelper *, X.TagHelpers

:

@addTagHelper *, X
+2

Source: https://habr.com/ru/post/1687775/


All Articles