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.
When we are done our solution will look like this.
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.
For reference, this post uses the following commands from the
Creates a new project, configuration file, or solution based on the specified template.
dotnet new <template> --name <projectName>
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:
At this point, the newly created .sln file doesn’t have any associated projects. Other than some default build configurations, it’s empty.
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.
Now we have a project we can add it to our solution file:
To create the web app we can do the same again, but this time using the mvc template.
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:
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.
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:
- Visual Studio on Windows and macOS
- Visual Studio code on Windows, macOS, and Linux
- Any other code editor with the .NET CLI on Windows, macOS, and Linux
One more command…
Finally, let’s run
dotnet sln list to list all the projects in our solution file.