Beginner 7 min read

The dotnet CLI

Dotnet CLI, .NET CLI commands, dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, .NET project templates, .NET command line, C# development, ASP.NET Core CLI, .NET SDK

The .NET CLI: Essential Commands and Project Templates

The .NET Command-Line Interface (CLI) is a collection of commands that helps developers create, build, run, test, and publish .NET applications directly from a terminal. It comes with the .NET SDK and provides a consistent development workflow across supported operating systems.

Whether you are building a C# console application, an ASP.NET Core Web API, or a reusable class library, the .NET CLI lets you perform essential development tasks without depending entirely on a graphical IDE. Learning these commands is especially useful when working with Visual Studio Code, automation scripts, and CI/CD pipelines.

1. What Is the .NET CLI?

The .NET CLI is a command-line tool for working with .NET projects and solutions. It provides commands for project creation, dependency restoration, compilation, execution, testing, and deployment preparation.

To verify that the .NET SDK is installed, open your terminal and run:

dotnet --version

To see detailed information about your environment, use:

dotnet --info

The .NET SDK includes the CLI, build tools, and framework components required for development. You need an appropriate SDK to create and build projects targeting your chosen .NET version.

2. Creating Projects with dotnet new

The dotnet new command creates a project from an installed template. Templates provide a predefined project structure, configuration files, and starter code, allowing you to begin development without creating every file manually.

Create a console application:

dotnet new console -n MyFirstApp
cd MyFirstApp

Create an ASP.NET Core Web API:

dotnet new webapi -n MyFirstApi

Create a class library:

dotnet new classlib -n MyBusinessLogic

In these examples, -n specifies the project name.

Exploring .NET templates

To list available templates, run:

dotnet new list

To search for templates using a keyword:

dotnet new list webapi

To display the options supported by a particular template, use:

dotnet new webapi --help

Templates help developers maintain consistent project structures and quickly create applications for different purposes.

3. Building Applications with dotnet build

The dotnet build command compiles a project or solution and reports compilation errors and warnings. It also performs dependency restoration when necessary, unless restoration is disabled or otherwise controlled.

Run the following command from your project directory:

dotnet build

A successful build confirms that the application has compiled successfully for the selected target framework and configuration. It does not guarantee that the application behaves correctly at runtime.

To build without restoring dependencies again, use:

dotnet build --no-restore

This option is useful in automated workflows where dependencies have already been restored.

4. Running Applications with dotnet run

The dotnet run command runs a .NET project from its source code. When necessary, it builds the application before execution.

For a console application, use:

dotnet run

For an ASP.NET Core Web API, navigate to the directory containing its project file and run the same command.

dotnet run

The application starts using its configured launch settings and environment. For a web application, the terminal typically displays the local URLs on which the application is listening.

You can also pass application arguments after --:

dotnet run -- --help

The double hyphen separates CLI options from arguments passed to the application. The example passes --help to the application itself; whether it displays help depends on the application's implementation.

5. Running Tests with dotnet test

The dotnet test command executes tests in a .NET test project. It is commonly used with testing frameworks such as xUnit, NUnit, and MSTest.

For example:

dotnet test

Run this command from the directory containing the relevant test project or solution.

If you do not have a test project, you can create one using an appropriate template. For example, with the xUnit template:

dotnet new xunit -n MyApp.Tests
dotnet test MyApp.Tests

Automated tests help verify that application components behave as expected and reduce the risk of introducing regressions when code changes.

For larger applications, running tests as part of a CI/CD pipeline helps detect problems before changes are deployed.

6. Publishing Applications with dotnet publish

The dotnet publish command prepares an application and its required deployment files for distribution or hosting. Unlike a simple build, publishing creates an output intended to be deployed to a target environment.

Run:

dotnet publish -c Release

The -c Release option selects the Release configuration, which is commonly used for production deployments.

For example, publish an ASP.NET Core application for a specific framework:

dotnet publish -c Release -f net10.0

Replace net10.0 with the target framework specified in your project file.

By default, publishing creates deployment files under a directory within bin/Release. The exact output path depends on the project and publishing options.

For deployments where the target machine does not have the required .NET runtime installed, you can publish a self-contained application for a supported runtime identifier. For example:

dotnet publish -c Release -r win-x64 --self-contained true

This example targets 64-bit Windows. Choose the appropriate runtime identifier for your deployment environment.

Important: Publishing does not automatically deploy your application to a server. It prepares the files that you can deploy using your chosen hosting or deployment process.

7. Common .NET CLI Commands at a Glance

CommandPurpose
dotnet --versionDisplays the selected SDK version.
dotnet --infoDisplays SDK, runtime, and environment details.
dotnet newCreates a project from a template.
dotnet new listLists available project templates.
dotnet restoreRestores project dependencies.
dotnet buildCompiles a project or solution.
dotnet runBuilds and runs a project when needed.
dotnet testExecutes tests in a test project.
dotnet publishPrepares application files for deployment.
dotnet cleanRemoves previous build outputs.

Understanding these commands gives you a practical foundation for developing .NET applications from the terminal and working with automated build systems.

Key Takeaways

  • The .NET CLI provides command-line tools for the complete application development workflow.
  • dotnet new creates projects from templates, including console applications, Web APIs, and class libraries.
  • dotnet build compiles your project and reports build errors.
  • dotnet run executes your application locally.
  • dotnet test runs automated tests to verify application behavior.
  • dotnet publish prepares application files for deployment.
  • Learning the .NET CLI improves productivity when using VS Code, scripting development tasks, and configuring CI/CD pipelines.

Frequently Asked Questions

What is the .NET CLI?

The .NET CLI is a command-line tool included with the .NET SDK. It allows developers to create, build, run, test, and publish .NET applications using terminal commands.

What is the difference between dotnet build and dotnet publish?

dotnet build compiles a project or solution for development and verification. dotnet publish prepares the application and its required files for deployment or distribution.

What does dotnet new do?

The dotnet new command creates a project or other supported artifact using a template. For example, dotnet new console creates a C# console application.

What is the purpose of dotnet test?

The dotnet test command runs automated tests in a test project. It helps developers verify application behavior and detect regressions when code changes.

Do I need Visual Studio to use the .NET CLI?

No. You can use the .NET CLI directly from a terminal with the appropriate .NET SDK installed. It works alongside editors such as VS Code and can also be used independently of a graphical IDE.

Related Topics

Last updated 10/26/2026

Your notes

Sign in to save personal notes on this topic — they'll sync across your devices.

Was this tutorial helpful?

Help us create more free learning resources. Learn more

Make a Donation