如何使用 dotnet cli 创建 .NET 5 版本的 webapi?

How could I create a webapi of the .NET 5 version using the dotnet cli?

如何使用 dotnet cli 创建 .NET 5 版本的 webapi?

每当我 运行 dotnet new webapi 我得到 .NET Core 3.1 项目。我在本地安装了 .NET 5。我怎样才能使 .NET 5 成为 dotnet cli 的默认版本?

我试过运行宁

$ dotnet new webapi --framework "net5.0"

但它给了我一个错误:

Couldn't find an installed template that matches the input, searching online for one that does...

这是 运行宁 dotnet --info 的结果:

$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.301
 Commit:    7feb845744

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk.1.301\

Host (useful for support):
  Version: 5.0.7
  Commit:  556582d964

.NET SDKs installed:
  3.1.301 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

只需输入“dotnet new webapi -f net5.0”。这应该可以解决问题

dotnet new webapi -o projectName --framework "net5.0"

注意:将 projectName 更改为您的项目名称。