【问题标题】:.NET Core - When to use "dotnet new sln".NET Core - 何时使用“dotnet new sln”
【发布时间】:2017-08-01 12:51:21
【问题描述】:

我有点困惑——我读过的大部分 .NET Core 教程都没有提到“dotnet new sln”——他们总是单独创建项目,没有任何解决方案文件将它们链接在一起。

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

  2. 我应该什么时候使用它?我从创建 .sln 文件而不是仅仅拥有项目文件中获得什么好处?它主要用于在 Visual Studio 中打开吗?我使用 Visual Studio Code for Mac,所以它可能不适用。

我搜索了“dotnet new sln”,结果非常少。

【问题讨论】:

标签: asp.net-core .net-core


【解决方案1】:

“dotnet new sln”是新命令吗?

是的。在 dotnet 命令行界面的 1.0.1 版本中,有一个 dotnet new sln 命令。该命令附带the change from project.json to 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

我应该什么时候使用这个?

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

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

创建 .sln 文件而不是只创建项目文件有什么好处?它主要用于在 Visual Studio 中打开吗?我使用 Visual Studio Code for Mac,所以它可能不适用。

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

例如,在装有 Visual Studio Code 的 Mac 上,我们可以使用 dotnet CLI 创建新的解决方案、创建一些项目、将这些项目添加到解决方案、恢复解决方案并构建解决方案。

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

调用dotnet build 的最后一个命令具有构建解决方案中所有项目的好处。如果没有解决方案,我们需要在每个项目上调用 dotnet build

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

【讨论】:

  • 您会推荐使用类库项目及其相关测试项目的解决方案吗?
  • @HoriaComan 为此使用解决方案似乎是合理的,是的。
  • 我们可以在 mvc 项目中使用dotnet run -p Foo\Foo.csproj 吗?似乎只有在我这样做时才有效cd Foo && dotnet run
  • @VincentDeSmet dotnet run -p .\Foo\Foo.csproj 对我有用。
  • 它抱怨我的子文件夹中没有 config.Development.json
猜你喜欢
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多