Profile picture of Liam Moat

Liam Moat

Principal Software Engineer at Microsoft

Create a Visual Studio solution using the dotnet CLI

By Liam Moat. . 3 minutes read.

The .NET Core command-line interface (CLI) is a cross-platform toolchain for developing .NET applications. This post will explore creating a Visual Studio solution using the CLI without needing to rely on Visual Studio.

For the purpose of this demo, we will create a Visual Studio solution that comprises of three projects; an ASP.NET Web App, a class library, and a unit test project. We will use the following directory structure and add references between the appropriate projects.

/demo
|__LiamMoat.Demo.sln
|__/src
    |__/LiamMoat.Demo.Core
      |__Source Files
      |__LiamMoat.Demo.Core.csproj
    |__/LiamMoat.Demo.WebApp
      |__Source Files
      |__LiamMoat.Demo.WebApp.csproj
/test
   |__/LiamMoat.Demo.Core.Tests
      |__Source Files
      |__LiamMoat.Demo.Core.Tests.csproj

When we are done our solution will look like this.

Solution explorer with project references

Note: Make sure you have dotnet installed by running dotnet --version. If you haven’t, take a look at Microsoft’s guide to Get started with .NET in 10 minutes.

Command reference

For reference, this post uses the following commands from the dotnet cli.

dotnet new

Creates a new project, configuration file, or solution based on the specified template.

dotnet new <template> --name <projectName>

dotnet sln

Modifies a .NET Core solution file.

dotnet sln <sln> add <project>

dotnet add reference

Adds project-to-project (P2P) references.

dotnet add <project> reference <projectReference>

Creating a new solution file

Before we create a project or write any code, let’s create a new solution file and two project folders - one for our source and another for test projects:

$ dotnet new sln --name LiamMoat.Demo
$ mkdir src
$ mkdir test

At this point, the newly created .sln file doesn’t have any associated projects. Other than some default build configurations, it’s empty.

Class Library

The first project we will create is a C# class library. cd into the src directory and then, with dotnet new, create the project using the classlib template.

$ cd src
$ dotnet new classlib --name LiamMoat.Demo.Core

Now we have a project we can add it to our solution file:

$ dotnet sln ..\LiamMoat.Demo.sln add .\LiamMoat.Demo.Core\LiamMoat.Demo.Core.csproj

Web App

To create the web app we can do the same again, but this time using the mvc template.

$ dotnet new mvc --name LiamMoat.Demo.Web
$ dotnet sln ..\LiamMoat.Demo.sln add .\LiamMoat.Demo.Web\LiamMoat.Demo.Web.csproj

We want the web app to reference our core class library. We can use dotnet add reference to add a project-to-project (P2P) reference to our web project:

$ dotnet add .\LiamMoat.Demo.Web reference .\LiamMoat.Demo.Core\LiamMoat.Demo.Core.csproj

Unit Test

The final project is for our unit tests and will use the mstest template. As we’re testing the core class library, let’s add a reference to that too. cd into the test directory, and create a project using the mstest template.

$ cd test
$ dotnet new mstest --name LiamMoat.Demo.Core.Test
$ dotnet sln ..\LiamMoat.Demo.sln add .\LiamMoat.Demo.Core.Test\LiamMoat.Demo.Core.Test.csproj
$ dotnet add .\LiamMoat.Demo.Core.Test reference ..\src\LiamMoat.Demo.Core\LiamMoat.Demo.Core.csproj

Summary

Using the dotnet command-line interface we can create, manage and interrogate a Visual Studio solution. With just a few commands we can create a solution, add a few projects and manage the references between them. The best bit… because we’re using .NET Core, this is cross-platform. You can use this solution file anywhere:

One more command

Finally, let’s run dotnet sln list to list all the projects in our solution file.

Project reference(s)
--------------------
src
src\LiamMoat.Demo.Core\LiamMoat.Demo.Core.csproj
src\LiamMoat.Demo.Web\LiamMoat.Demo.Web.csproj
test
test\LiamMoat.Demo.Core.Test\LiamMoat.Demo.Core.Test.csproj