如何将库项目中的 ASP.NET Core 3.0 类型用于共享控制器、中间件等? [英] How do I use ASP.NET Core 3.0 types from a library project for shared Controllers, Middleware etc?

查看:15
本文介绍了如何将库项目中的 ASP.NET Core 3.0 类型用于共享控制器、中间件等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然可以通过 NuGet 使用高达 2.2 的 ASP.NET Core 来为共享控制器、中间件等创建库项目,但如何创建能够使用 ASP.NET Core 3.0 类型的库?

While ASP.NET Core up to 2.2 could be consumed through NuGet to create library projects for shared Controllers, Middleware etc, how do I create a library that is able to use ASP.NET Core 3.0 types?

虽然对于包含视图的项目有一个Razor 类库"(razorclasslib) 模板,但我如何创建一个只包含逻辑组件的库?

While for projects containing views there is a "Razor Class Library" (razorclasslib) template, how do I create a library that only contains logic components?

推荐答案

为 .NET Core 3.0 构建的应用程序可以引用一个或多个共享框架.ASP.NET Core 是这些共享框架之一(其他可能是基础 .NET Core 共享框架和包含 WinForms 和 WPF 的 Windows 桌面共享框架).

Applications built for .NET Core 3.0 can reference one or more shared frameworks. ASP.NET Core is one of these shared frameworks (others would be the base .NET Core Shared framework and the Windows Desktop Shared Framework containing WinForms and WPF).

要从面向 .NET Core 3.0(netcoreapp3.0,而不是 .NET Standard)的经典 .NET Core 库中引用 ASP.NET Core,您可以使用 FrameworkReference 在 csproj 中引用框架:

To reference ASP.NET Core from a classic .NET Core library targeting .NET Core 3.0 (netcoreapp3.0, not .NET Standard), you can use a FrameworkReference in the csproj to reference the framework:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>

在 Visual Studio 中打开时,此附加框架引用将显示在解决方案资源管理器的依赖项节点中:

When opened in Visual Studio, this additional framework reference will show up in the dependencies node in solution explorer:

这篇关于如何将库项目中的 ASP.NET Core 3.0 类型用于共享控制器、中间件等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆