.NET 核心-何时使用“ dotnet new sln”

我有点困惑。NET 核心教程我一直在阅读没有提到“ dotnet 新的 sln”-他们总是只是创建项目单独没有任何解决方案文件链接到一起。

  1. “ dotnet new sln”是新命令吗?

  2. 我什么时候用这个?创建一个。而不是只有项目文件?主要用于在 Visual Studio 中打开吗?我为 Mac 使用 VisualStudio 代码,所以它可能不适用。

我在谷歌上搜索了“ dotnet new sln”,结果非常有限。

38093 次浏览

Is "dotnet new sln" a new command?

Yes. In version 1.0.1 of the dotnet command line interface, there is a dotnet new sln command. The command came with the change from project.json to csproj. If we run dotnet new --help, we will see "Solution File" as one of the templates.

> dotnet new --help


Templates                 Short Name      Language      Tags
----------------------------------------------------------------------
Console Application       console         [C#], F#      Common/Console
Class library             classlib        [C#], F#      Common/Library
Unit Test Project         mstest          [C#], F#      Test/MSTest
xUnit Test Project        xunit           [C#], F#      Test/xUnit
ASP.NET Core Empty        web             [C#]          Web/Empty
ASP.NET Core Web App      mvc             [C#], F#      Web/MVC
ASP.NET Core Web API      webapi          [C#]          Web/WebAPI
Solution File             sln                           Solution

when should I use this?

Two times to use a solution file are:

  1. when we want to use Visual Studio, and/or
  2. when we want to manage several projects as a single unit.

What benefits do I gain from creating a .sln file instead of just having project files? Is it mainly for opening in Visual Studio? I use Visual Studio Code for Mac, so it may not be applicable.

One of the benefits that do not require Visual Studio is the management of multiple projects as a single unit.

For instance, on a Mac with Visual Studio Code, we can use the dotnet CLI to create a new solution, create a few projects, add those projects to the solution, restore the solution, and build the solution.

dotnet new sln --name FooBar
dotnet new console --name Foo --output Foo
dotnet new console --name Bar --output Bar
dotnet sln add .\Foo\Foo.csproj
dotnet sln add .\Bar\Bar.csproj
dotnet restore
dotnet build FooBar.sln

The last command, which calls dotnet build, has the benefit of building all the projects that are in the solution. Without a solution, we would need to call dotnet build on each project.

There are no doubt other benefits which do not require the use of Visual Studio. I leave those to you to discover.