c# - 在 ASP.NET Core 3 的类库中创建 TagHelper

标签 c# asp.net asp.net-core asp.net-core-tag-helpers

如何使用对 ASP.NET Core 3 的更改在单独的类库中创建 TagHelper 类?

在 ASP.NET Core 1.x 和 2.x 中,您可以仅引用 NuGet 包 Microsoft.AspNetCore.Razor 以允许您的类派生自 Microsoft.AspNetCore。 Razor.TagHelpers.TagHelper.

在ASP.NET Core 3.x中,似乎只有一个名为Microsoft.AspNetCore.App的大包,在NuGet上找不到

该解决方案应支持在 ASP.NET Core 3 和更早版本 中使用包含的 TagHelper 类。

最佳答案

看看Migrate from ASP.NET Core 2.2 to 3.0 .

它解释了仅引用 Microsoft.NET.Sdk.Razor 的项目(例如您的类库)需要添加对 Microsoft.AspNetCore.App 的引用。但是,对于面向 Microsoft.NET.Sdk.Web(例如 MVC 应用程序或 WebAPI)的项目,这会自动包含在内。

引自FrameworkReference部分(第二个项目符号适用于您的项目):

Features of ASP.NET Core that were available through one of the packages listed above are available as part of the Microsoft.AspNetCore.App shared framework. The shared framework is the set of assemblies (.dll files) that are installed on the machine and includes a runtime component, and a targeting pack. For more information, see The shared framework.

  • Projects that target the Microsoft.NET.Sdk.Web SDK implicitly reference the Microsoft.AspNetCore.App framework. No additional references are required for these projects:

    <Project Sdk="Microsoft.NET.Sdk.Web">
      <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
      </PropertyGroup>
      ...
    </Project>
    
  • Projects that target Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK, should add an explicit FrameworkReference to Microsoft.AspNetCore.App:

    <Project Sdk="Microsoft.NET.Sdk.Razor">
      <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
      </ItemGroup>
      ...
    </Project>
    

关于c# - 在 ASP.NET Core 3 的类库中创建 TagHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58539289/

相关文章:

c# - MSBuild 和 <AutoUnifyAssemblyReferences/>

.net - 如何将CMS添加到现有网站

c# - 最小化所有形式的类

c# - List<T> 是链表吗?

asp.net - web.config 和成员(member)数据库 - 从开发到生产

c# - 使用 Entity Framework 在 ASP.NET Core 应用程序中对远程用户进行 Windows 身份验证

asp.net-core - Blazor WebAssembly 托管代理在成功身份验证时崩溃

c# - 如何在 Tag Helper 的 ASP.NET Core ProcessAsync 中强制更改线程

c# - WebCamTexture 不显示在 Unity3D 中?

asp.net - 使用过滤器 : alpha(opacity:0) 时,IE7 并不总是呈现控件不可见