Useful .NET Core SDK CLI commands
Commands in the current post are extraction from Build a REST API with .NET Core 2 and run it on Docker Linux container and .NET Core integration testing and mock dependencies posts where I have used them in a real project.
All commands
All available commands are accessed with: dotnet --help.Initialise .NET projects
Creating new projects is done with: dotnet new [options]. I have used following commands:- Create new console application project: dotnet new console -o <ProjectName>
- Create new MS Test project: dotnet new mstest -o <ProjectName>
- Create new solution file: dotnet new sln –name <SolutionName>
Custom templates
.NET SDK allows you to create custom project template and then install it to SDK with the command: dotnet new -i <TEMPLATE_FOLDER>. Once installed can create a new project out of your custom template. This is valuable in big organizations where cohesion is needed between similar project types. See more for templates in Custom templates for dotnet new article.Manage dependencies
Two types of dependencies are available, in a NuGet package or to another .NET project.- Add reference to a NuGet package: dotnet add package <NuGetPackageName>
- Add reference to another .NET project: dotnet add reference <PathToProjectFile>
Project dependencies can be shown with: dotnet list reference <PathToProjectFile>.
Actions on a project
- Build a project: dotnet build
- Run a project: dotnet run
- Run tests of a project: dotnet test
- Publish project artefacts: dotnet publish
Manage solution file
- Create new solution file: dotnet new sln –name <SolutionName>
- Add project to solution: dotnet sln <SolutionFileName> add <PathToProjectFile>
- Remove project from solution: dotnet sln <SolutionFileName> remove <PathToProjectFile>
- List projects in a solution: dotnet sln <SolutionFileName> list