.NET CLI Documentation

Introduction to the .NET CLI

The .NET Command Line Interface (CLI) is a powerful, cross-platform toolset for developing, building, running, and publishing .NET applications. It allows you to interact with your .NET projects and solutions from the terminal or command prompt, providing a consistent experience across Windows, macOS, and Linux.

With the .NET CLI, you can:

Getting Started

To get started with the .NET CLI, ensure you have the .NET SDK installed on your machine. You can download it from the official dotnet.microsoft.com/download website.

Once installed, you can verify the installation by opening your terminal and running:

dotnet --version

To create a new console application, navigate to your desired directory and run:

dotnet new console -o MyConsoleApp

This will create a new directory named MyConsoleApp containing a basic console project structure.

Common Commands

dotnet new

Creates a new .NET project or solution from a template.

dotnet new <TEMPLATE> [-o|--output] [-n|--name] [--force] [--list]

Common templates include:

Example: Create a new ASP.NET Core MVC project:

dotnet new mvc -o MyMvcApp -n MyWebApp

To list available templates, use:

dotnet new --list

dotnet build

Compiles a .NET project.

dotnet build [<PROJECT|SOLUTION|FOLDER>] [--configuration|--cfg <CONFIGURATION>] [--output|--out <OUTPUT_DIRECTORY>] [--no-restore]

Example: Build the current project in Release configuration:

dotnet build --configuration Release

dotnet run

Compiles and runs a .NET project.

dotnet run [<PROJECT|SOLUTION|FOLDER>] [--configuration|--cfg <CONFIGURATION>] [--project-args <ARGUMENTS>] [--no-build] [--no-restore]

Example: Run the current project:

dotnet run

dotnet publish

Publishes a .NET project for deployment.

dotnet publish [<PROJECT|SOLUTION|FOLDER>] [--configuration|--cfg <CONFIGURATION>] [--output|--out <OUTPUT_DIRECTORY>] [--runtime <RUNTIME_IDENTIFIER>] [--no-restore] [--self-contained [true|false]]

Example: Publish a self-contained Windows x64 application:

dotnet publish --runtime win-x64 --configuration Release --self-contained true

dotnet test

Runs unit tests for a .NET project.

dotnet test [<PROJECT|SOLUTION|FOLDER>] [--configuration|--cfg <CONFIGURATION>] [--logger <LOGGER>] [--filter <EXPRESSION>] [--no-restore]

Example: Run tests and output results to a specific logger:

dotnet test --logger "console;verbosity=detailed"

dotnet add

Adds a reference to a project or package.

dotnet add <PROJECT|SOLUTION> reference <PROJECT_TO_ADD>
dotnet add <PROJECT|SOLUTION> package <PACKAGE_NAME> [--version <VERSION>]

Example: Add a NuGet package:

dotnet add MyProject.csproj package Newtonsoft.Json

dotnet remove

Removes a reference to a project or package.

dotnet remove <PROJECT|SOLUTION> reference <PROJECT_TO_REMOVE>
dotnet remove <PROJECT|SOLUTION> package <PACKAGE_NAME>

Example: Remove a NuGet package:

dotnet remove MyProject.csproj package Newtonsoft.Json

dotnet restore

Restores the .NET project's dependencies.

dotnet restore [<PROJECT|SOLUTION|FOLDER>] [--configuration|--cfg <CONFIGURATION>] [--no-cache]

This command is usually run automatically by other commands like build and run, but can be run explicitly.

dotnet restore

dotnet list

Lists projects or packages in a project or solution.

dotnet list [<PROJECT|SOLUTION|FOLDER>] package [<PACKAGE_NAME>]
dotnet list [<PROJECT|SOLUTION|FOLDER>] reference

Example: List all NuGet packages in the current project:

dotnet list package

Command Structure

The general structure of a .NET CLI command is:

dotnet [<COMMAND>] [<ARGUMENTS>] [--options]

Options and Arguments

Commands can take positional arguments and named options. Positional arguments are typically required and their meaning depends on the command.

Options, denoted by double hyphens (--) or single hyphens (-) for short forms, modify the command's behavior. For example:

Many commands also accept global options that apply to all `dotnet` commands, such as:

To see all available options for a specific command, use the --help flag:

dotnet new --help

Global Options

These options can be used with any dotnet command:

Example: Get detailed information about the .NET installation:

dotnet --info

More Examples

Create a new web API project in a specific directory:

dotnet new webapi -o src/MyApi -n MyAwesomeApi

Restore dependencies for a solution file, build it in Release configuration, and then publish it to a specific folder:

dotnet restore MySolution.sln
dotnet build MySolution.sln --configuration Release
dotnet publish MySolution.sln --configuration Release --output ./publish

Troubleshooting

Note:

If you encounter issues, first try running the command with the --verbose option to get more detailed output.