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

时间:2017-03-11 03:06:14

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

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

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

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

  3. 我用google搜索“dotnet new sln”,结果很少。

1 个答案:

答案 0 :(得分:95)

  

是“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. 当我们想要将多个项目作为一个单元进行管理时。
  3.   

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

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

    例如,在使用VS 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。我把这些留给你去发现。

相关问题