The CLI of .NET is a command-line interface provided by the .NET SDK that allows performing tasks and actions to manage .NET projects.
With the CLI we can create, compile, run, and manage .NET projects without needing a graphical environment (like Visual Studio).
This is often faster and more efficient than doing it from an IDE. Additionally, it allows us to automate tasks and workflows.
| Feature | CLI | Visual Studio |
|---|---|---|
| Speed | 🟢 Fast | 🔴 Slower |
| Control | 🟢 Granular | 🔴 Limited |
| Automation | 🟢 Excellent | 🔴 Limited |
| Interface | Terminal | GUI |
| Requirements | 🟢 SDK only | 🔴 Full IDE |
The .NET SDK CLI is like your C# developer’s Swiss Army knife. It provides you with a complete set of utilities to perform any development task for applications in the .NET environment.
Besides, using the console makes you feel like a hacker, which isn’t bad
Since it’s a tool you’ll use frequently, let’s see how to use it 👇
Verify .NET SDK Installation
Before starting, we should obviously verify that the .NET SDK is correctly installed. To do this, run the following command:
dotnet —version
This command will show the installed SDK version. If you don’t see a version, make sure the SDK is correctly installed and that the CLI path is in your PATH environment variable.
If you don’t have .NET SDK installed, we covered it in
Basic Command Structure
The .NET CLI is a command-line interface. That is, it runs and responds through a terminal (like CMD, or PS).
All commands look more or less the same and follow the pattern:
dotnet [command] [arguments] [options]
Some of the most common commands are,
| Command | Description | Example |
|---|---|---|
new | Creates new projects | dotnet new console |
restore | Restores dependencies | dotnet restore |
build | Compiles the project | dotnet build |
run | Runs the project | dotnet run |
test | Runs tests | dotnet test |
add | Adds packages/references | dotnet add package Newtonsoft.Json |
publish | Publishes for deployment | dotnet publish -c Release |
Project Creation
One of the most common functions of the CLI is creating a new project, with the dotnet new command. This command accepts predefined templates for different project types.
For example, if we want to create a new web API project we would do this,
dotnet new webapi -n MiPrimeraApi
- webapi: The type of project we want to create (console, wepapi…)
- -n: Project name
- -o: Output directory
- -f: .NET version
This command will create a folder called MyFirstApi with the basic structure of a web API project.
Dependency Management
With the CLI we can also manage our project’s dependencies.
Add NuGet Packages
NuGet packages are external libraries you can add to your project to extend its functionality. To add a package, we use the dotnet add package command.
For example, to add the Microsoft.EntityFrameworkCore package:
dotnet add package Microsoft.EntityFrameworkCore
This command will download and install the package into your project, and automatically add it to the .csproj file.
Reference Other Projects
We can also add a reference to another project,
dotnet add reference ../MyClassLib/MyClassLib.csproj
Restore Dependencies
When you clone an existing project or add new packages, you may need to restore dependencies. To do this, use the dotnet restore command.
dotnet restore
This command will download all dependencies needed to compile and run the project.
Build and Run
Of course, we can also compile and run our project.
Run a Project
To run an ASP.NET Core project, use the dotnet run command. This command compiles the project (if necessary) and runs it.
dotnet run
Compile a Project
Once you’ve created a project, you can compile it using the dotnet build command. This command compiles the source code and generates the necessary binary files to run the application.
dotnet build
If the project compiles successfully, you’ll see a success message in the terminal. If there are errors, the CLI will show you details about the problems encountered.
Useful Options:
- -c|—configuration: Debug/Release (-c release)
- -r|—runtime: Specify RID Runtime Identifier (-r linux-x64)
- —no-restore: Skips restoration
If you need to pass parameters to the executable, you can do it like this
dotnet run — arg1 arg2
For example
dotnet run —urls=http://localhost:8080
Clean the Project
If you want to delete the files generated during compilation, you can use the dotnet clean command.
dotnet clean
This command will delete the bin and obj folders, which contain the binary and temporary files generated during compilation.
Run Tests
If your project includes unit tests, you can run them using the dotnet test command.
dotnet test
This command will search for all test projects in the solution and run the tests.
Publishing an Application
When you’re ready to deploy your application, you can use the dotnet publish command to generate the necessary files for publishing.
dotnet publish -c Release -o ./publish
This command compiles the project in Release mode and places the resulting files in the ./publish folder.
To distribute a compiled application:
dotnet publish -c Release -r win-x64 --self-contained true
This generates a self-contained executable for Windows. You can change win-x64 to linux-x64 or osx-x64 depending on the target system.
Global Tools
The CLI also allows us to install global tools. These are NuGet packages that contain console applications that can be installed and run from anywhere on your machine.
These tools are useful for development tasks, automation, or any utility you need to have available globally.
| Command | Description |
|---|---|
dotnet tool install -g <ToolName> | Installs a tool globally |
dotnet tool update -g <ToolName> | Updates a global tool |
dotnet tool list -g | Lists all globally installed tools |
dotnet tool uninstall -g <ToolName> | Uninstalls a global tool |
For example, if we want to install the dotnet-ef tool to handle Entity Framework projects, we would do this
Install the ‘dotnet-ef’ tool globally
dotnet tool install -g dotnet-ef
Run the tool
dotnet ef
