.NET Core Command-Line Interface (CLI)
The .NET Core Command-Line Interface (CLI) is a cross-platform tool used to create, restore packages, build, run, and publish .NET applications.
In the previous chapter, we created our first ASP.NET Core application using Visual Studio. Internally, Visual Studio leverages the CLI to handle tasks like restoring dependencies, building the solution, and publishing the app. Other IDEs, editors, and tools can also use the CLI to work with .NET Core applications.
The CLI is included with the .NET Core SDK, so there’s no need for a separate installation. To verify if the CLI is installed on your system:
-
Open the Command Prompt (on Windows).
-
Type
dotnet
and press Enter. -
If the usage and help information is displayed, the CLI is installed correctly.
CLI Command Structure
The general structure of a .NET Core CLI command is:
-
dotnet
: The CLI driver that initiates the command. -
<command>
: The specific action to perform (e.g.,build
,run
,restore
). -
<arguments>
and<options>
: Parameters and modifiers for the command.
Common CLI Commands
Basic Commands
Command | Description |
---|---|
new | Creates a new project, configuration file, or solution from a template. |
restore | Restores project dependencies and tools. |
build | Builds the project and its dependencies. |
run | Runs the source code directly. |
publish | Packages the app and its dependencies for deployment. |
test | Executes unit tests in the project. |
vstest | Runs tests from specified test assemblies. |
pack | Packs code into a NuGet package. |
clean | Cleans the build output of the project. |
sln | Modifies a .NET Core solution file. |
help | Displays help information about CLI commands. |
store | Stores specified assemblies in the runtime package store. |
Project Modification Commands
Command | Description |
---|---|
add package | Adds a NuGet package reference. |
add reference | Adds a project-to-project (P2P) reference. |
remove package | Removes a NuGet package reference. |
remove reference | Removes a P2P reference. |
list reference | Lists all P2P references in the project. |
Advanced Commands
Command | Description |
---|---|
nuget delete | Deletes or unlists a package from the NuGet server. |
nuget locals | Clears or lists local NuGet resources. |
nuget push | Pushes a package to the NuGet server. |
msbuild | Builds a project using MSBuild. |
install script | Installs .NET Core CLI tools and the shared runtime. |
Working with CLI: Example
Let’s walk through creating, restoring, building, and running a .NET Core console application using the CLI (without Visual Studio).
1. Create a New Project
To create a new console project in the current directory:
To create a new project named MyConsoleApp:
To create it in a specific directory (e.g., C:\MyProjects
):
Navigate to the project directory before running additional commands:
2. Add a NuGet Package
To add the Newtonsoft.Json
package:
You can confirm the package reference by opening the .csproj
file.
3. Restore Packages
To restore project dependencies:
4. Build the Project
To compile the project:
5. Run the Project
To execute the application:
Getting Help
To display help for any command, append -h
or --help
. For example:
This will show available templates, arguments, and options for the new
command.