How do I use ASP.NET Core 3.0 types from a library project for shared Controllers, Middleware etc?

痴心易碎 提交于 2020-02-26 07:09:28

问题


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?

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


回答1:


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).

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>

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



来源:https://stackoverflow.com/questions/57760356/how-do-i-use-asp-net-core-3-0-types-from-a-library-project-for-shared-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!