.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:

  1. Open the Command Prompt (on Windows).

  2. Type dotnet and press Enter.

  3. 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 <command> <arguments> <options>
  • 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

CommandDescription
newCreates a new project, configuration file, or solution from a template.
restoreRestores project dependencies and tools.
buildBuilds the project and its dependencies.
runRuns the source code directly.
publishPackages the app and its dependencies for deployment.
testExecutes unit tests in the project.
vstestRuns tests from specified test assemblies.
packPacks code into a NuGet package.
cleanCleans the build output of the project.
slnModifies a .NET Core solution file.
helpDisplays help information about CLI commands.
storeStores specified assemblies in the runtime package store.














Project Modification Commands

CommandDescription
add packageAdds a NuGet package reference.
add referenceAdds a project-to-project (P2P) reference.
remove packageRemoves a NuGet package reference.
remove referenceRemoves a P2P reference.
list referenceLists all P2P references in the project.








Advanced Commands

CommandDescription
nuget deleteDeletes or unlists a package from the NuGet server.
nuget localsClears or lists local NuGet resources.
nuget pushPushes a package to the NuGet server.
msbuildBuilds a project using MSBuild.
install scriptInstalls .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:

dotnet new console

To create a new project named MyConsoleApp:

dotnet new console -n MyConsoleApp

To create it in a specific directory (e.g., C:\MyProjects):

dotnet new console -n MyConsoleApp -o C:\MyProjects

Navigate to the project directory before running additional commands:

cd C:\MyProjects\MyConsoleApp

2. Add a NuGet Package

To add the Newtonsoft.Json package:

dotnet add package Newtonsoft.Json

You can confirm the package reference by opening the .csproj file.


3. Restore Packages

To restore project dependencies:

dotnet restore

4. Build the Project

To compile the project:

dotnet build

5. Run the Project

To execute the application:

dotnet run

Getting Help

To display help for any command, append -h or --help. For example:

dotnet new -h

This will show available templates, arguments, and options for the new command.

Leave a Reply

Your email address will not be published. Required fields are marked *


Talk to us?

Post your blog

F.A.Q

Frequently Asked Questions