gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:17:42 25 4
gpt4 key购买 nike

如何使用对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com