Custom tag helper not working

前端 未结 8 1525
抹茶落季
抹茶落季 2021-02-03 19:11

I followed a few guides on creating a custom tag helper for ASP Core.

This is my helper:

using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Asp         


        
8条回答
  •  天命终不由人
    2021-02-03 19:36

    And also keep in mind that at the moment (March 2020) .Net Core 3 automatically generates the namespaces with underscores in it. Nevertheless, the assembly name will be exactly the same as the folder name (even if it does contain whitespaces and other uncommon for folder name symbols). It can cause troubles with adding your custom tag helpers.

    Let's assume:

    1. You have a folder called SUPER-TEST
    2. You cd into it and call dotnet new mvc
    3. This makes your new project have a namespace "SUPER_TEST".
    4. You create a tag helper in this namespace and include the assembly name into the _ViewImports like this:
    ***
    @addTagHelper *, SUPER_TEST
    ***
    

    It's not gonna work. Because in fact your assembly is now called SUPER-TEST. .Net Core runtime replaces underscores with dashes when creating the new project.

    So, you have to import the tag helpers from SUPER-TEST, like this:

    ***
    @addTagHelper *, SUPER-TEST
    ***
    

提交回复
热议问题