.NET Core CLI (Command-Line Interface)
The .NET Core Command-Line Interface (CLI) is a cross-platform toolchain that enables you to develop, build, run, and publish .NET Core applications from the command line.
Introduction
The .NET Core CLI provides a consistent and powerful way to interact with your .NET Core projects and solutions. It is built into the .NET Core SDK, so if you have the SDK installed, you already have the CLI.
Key benefits of using the .NET Core CLI:
- Cross-platform: Works on Windows, macOS, and Linux.
- Fast and Efficient: Designed for speed and performance.
- Extensible: Supports custom commands and global tools.
- Integrated: Seamlessly works with project files (e.g.,
.csproj
).
Common Commands
Here are some of the most frequently used .NET Core CLI commands:
Project and Solution Management
dotnet new <template> [-o <output-dir>] [-n <name>]
Creates a new project or solution from a specified template (e.g., console
, webapi
, classlib
).
dotnet new sln [-n <name>]
Creates a new solution file.
dotnet add <project-path> reference <project-to-add-path>
Adds a project reference to another project.
dotnet add <project-path> package <package-name> [--version <version>]
Adds a NuGet package reference to a project.
dotnet remove <project-path> reference <project-to-remove-path>
Removes a project reference.
dotnet remove <project-path> package <package-name>
Removes a NuGet package reference.
Building and Running
dotnet build [<project-path>] [--configuration <config>]
Builds a project or solution. Common configurations are Debug
and Release
.
dotnet run [<project-path>] [--configuration <config>] [--no-build]
Builds and runs a project. The --no-build
flag skips the build step if the project is already built.
dotnet clean [<project-path>] [--configuration <config>]
Cleans the output directory of a project.
Publishing
dotnet publish [<project-path>] [--configuration <config>] [--output <output-dir>] [--runtime <runtime-id>] [--no-build]
Publishes a .NET Core application for deployment. You can publish self-contained or framework-dependent applications.
Testing
dotnet test [<project-path>] [--filter <expression>] [--list-tests]
Runs unit tests in a project.
Other Useful Commands
dotnet restore [<project-path>]
Restores the dependencies of a project.
dotnet pack [<project-path>] [--configuration <config>] [--output <output-dir>]
Creates a NuGet package from a project.
dotnet migrate [<project-path>]
Migrates projects from project.json
to .csproj
format.
dotnet --info
Displays information about the .NET installation.
dotnet --version
Displays the .NET CLI version.
Basic Usage
Most .NET Core CLI commands are executed from the directory containing your project file (e.g., .csproj
) or solution file (e.g., .sln
). If you are not in the correct directory, you can specify the project or solution path as an argument.
--help
flag with any command for detailed usage information. For example: dotnet new --help
.
Global Tools
Global tools are NuGet packages that contain executable commands. They are installed globally and can be run from any directory.
To install a global tool:
dotnet tool install --global <tool-name>
To update a global tool:
dotnet tool update --global <tool-name>
To list installed global tools:
dotnet tool list --global
Common global tools include:
dotnet-ef
: Entity Framework Core command-line tools.dotnet-gcdiag
: Global CA Diagnostics tool.
Project.json vs. .csproj
Historically, .NET Core used project.json
for project configuration. However, this has been replaced by the MSBuild-based .csproj
file format, which provides more flexibility and better integration with the .NET ecosystem.
The dotnet migrate
command can help convert older project.json
files to the modern .csproj
format.