简体   繁体   English

如何在新创建的.NET Core 2.0 Web应用程序中定位.NET Standard 2.0?

[英]How do I target .NET Standard 2.0 in a freshly created .NET Core 2.0 web app?

I've just created a fresh project using dotnet new web . 我刚刚使用dotnet new web创建了一个新项目。 My Google-foo may be failing me, but I didn't find anything relating to my answer (please link to another SO answer, or relevant documentation if I've missed something obvious). 我的Google-foo可能会让我失望,但我没有找到任何与我的答案相关的内容(请链接到另一个SO答案,或相关文档,如果我错过了一些明显的东西)。

If I want to ensure this new project is .NET Standard 2.0 compliant, what do I now do? 如果我想确保这个新项目符合.NET Standard 2.0,我现在该怎么办?

NET Standard is for class libraries. NET Standard适用于类库。 Applications must target netcoreapp* where * is a version number. 应用程序必须以netcoreapp *为目标,其中*是版本号。 The following shows the compatibility matrix: https://docs.microsoft.com/en-us/dotnet/standard/net-standard 以下显示了兼容性矩阵: https//docs.microsoft.com/en-us/dotnet/standard/net-standard

For example, .NET Core 2 can consume .NET Standard version 2 and below. 例如,.NET Core 2可以使用.NET Standard 2及更低版本。

It is not inherently possible to run a netstandard project as an executable. netstandard项目作为可执行文件运行本身并不是可能的。 Since netstandard was designed to be used for libraries. 由于netstandard旨在用于图书馆。

In order to develop your web application entirely in netstandard2.0 , you would have to create a separate project that targets either .NET Core or .NET Framework to execute your library that contains your web app (developed using .NET Standard) . 为了完全在netstandard2.0开发Web应用程序, 您必须创建一个单独的项目,该项目以.NET Core或.NET Framework为目标,以执行包含Web应用程序的库(使用.NET Standard开发)

1. Executable Project (ex: console app)
   -- Target Framework: netcoreapp2.0 / net462

2. Web Application Project (library)
   -- Target Framework: netstandard2.0

You can use the following steps to change the target framework of your project. 您可以使用以下步骤更改项目的目标框架。

Step 1. Target the desired framework 步骤1.定位所需的框架

  1. Right-click on your project and select Edit *****.csproj 右键单击您的项目,然后选择Edit *****.csproj

  2. In the .csproj file, you need to replace the target framework to the .NET Framework. .csproj文件中,您需要将目标框架替换为.NET Framework。

Example .csproj file: 示例.csproj文件:

<Project Sdk="Microsoft.NET.Sdk.Web"> //<-- note the .Web for the web template
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

For a list of the Target Framework Moniker (TFM) (ie, net47 , netstandard2.0 , netcoreapp2.0 , etc.*) you can check this link out: https://docs.microsoft.com/en-us/dotnet/standard/frameworks 有关Target Framework Moniker(TFM)的列表(即net47netstandard2.0netcoreapp2.0等*),您可以查看以下链接: httpsnetcoreapp2.0 /标准/框架

Step 2. Run dotnet restore 步骤2.运行dotnet restore

Go to your output window and run dotnet restore . 转到输出窗口并运行dotnet restore

Note: Sometimes Visual Studio may misbehave (depending on which update you have installed), so you may have to close and re-open your Visual Studio. 注意:有时Visual Studio可能会出现异常(取决于您安装的更新),因此您可能必须关闭并重新打开Visual Studio。 Otherwise, sometimes a clean/re-build may do the trick. 否则,有时干净/重建可能会成功。


Targeting both frameworks 针对两个框架

You can pick one or the other, or even target both frameworks. 您可以选择其中一个,甚至可以定位两个框架。

<TargetFrameworks>netcoreapp2.0; net47</TargetFrameworks> //<-- note the plural form!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM