.NET Core - 何时使用"dotnet new sln"

And*_*yNZ 65 .net-core asp.net-core

我有点困惑 - 我读过的大多数.NET Core教程都没有提到"dotnet new sln" - 它们总是单独创建项目,没有解决方案文件将它们链接在一起.

  1. 是"dotnet new sln"新命令?

  2. 我什么时候应该用这个?我从创建sln文件而不是仅仅拥有项目文件中获得了哪些好处?它主要用于在Visual Studio中打开吗?我使用Visual Studio代码用于mac,因此可能不适用.

我用google搜索"dotnet new sln",结果很少.

Sha*_*tin 110

是"dotnet new sln"新命令?

是.在dotnet命令行界面的1.0.1版中,有一个dotnet new sln命令.该命令随project.json更改为csproj.如果我们运行dotnet new --help,我们将看到"解决方案文件"作为模板之一.

> 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
Run Code Online (Sandbox Code Playgroud)

我什么时候应该用这个?

两次使用解决方案文件是:

  1. 当我们想要使用Visual Studio和/或
  2. 当我们想要将多个项目作为一个单元进行管理时.

我从创建sln文件而不是仅仅拥有项目文件中获得了哪些好处?它主要用于在Visual Studio中打开吗?我使用Visual Studio代码用于mac,因此可能不适用.

不需要Visual Studio的一个好处是将多个项目作为一个单元进行管理.

例如,在使用VS Code的Mac上,我们可以使用dotnetCLI创建新解决方案,创建一些项目,将这些项目添加到解决方案,还原解决方案以及构建解决方案.

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
Run Code Online (Sandbox Code Playgroud)

最后一个调用的命令dotnet build可以构建解决方案中的所有项目.如果没有解决方案,我们需要调用dotnet build每个项目.

毫无疑问,其他好处不需要使用Visual Studio.我把这些留给你去发现.