The .NET CLI (Command-Line Interface) is a powerful and flexible tool that enables developers to create, build, run, test, and publish .NET applications directly from the command line. Whether you're working on Windows, macOS, or Linux, the .NET CLI allows you to streamline your development workflow without relying solely on Visual Studio or any graphical user interface.
This comprehensive guide will help you understand how the .NET CLI works, how to install it, and how to use it effectively for various types of .NET projects such as console applications, web APIs, libraries, and more.
The .NET CLI is a cross-platform toolchain for developing .NET applications. It is available with the .NET SDK and enables command-line operations for managing your entire development lifecycle, including project creation, code compilation, testing, and deployment.
To use the .NET CLI, you need to install the .NET SDK, which includes the CLI tool.
winget install Microsoft.DotNet.SDK.7
sudo apt-get update
sudo apt-get install -y dotnet-sdk-7.0
dotnet --version
dotnet --info
Every .NET CLI command follows this general structure:
dotnet <command> [arguments] [options]
dotnet new console -n HelloWorldApp
Use this command to create new projects from templates.
dotnet new console -n MyApp
dotnet new webapi -n MyWebApi
dotnet new classlib -n MyLibrary
dotnet new mvc -n MyMvcApp
dotnet new --list
This command builds the source code into binaries.
cd MyApp
dotnet build
Execute the compiled application from the source code.
dotnet run
This command creates a self-contained or framework-dependent deployment.
dotnet publish -c Release -o ./publish
Run test projects and get test results.
dotnet new xunit -n MyTests
cd MyTests
dotnet test
Add NuGet packages or project references to a project.
dotnet add package Newtonsoft.Json
dotnet add reference ../MyLibrary/MyLibrary.csproj
Restores NuGet dependencies specified in the project file.
dotnet restore
Deletes all build artifacts from the project directory.
dotnet clean
dotnet new console -n MyConsoleApp
cd MyConsoleApp
File: Program.cs
using System;
Console.WriteLine("Hello, CLI World!");
dotnet build
dotnet run
dotnet new webapi -n MyWebApi
cd MyWebApi
dotnet run
Access your API at https://localhost:5001
The global.json file allows you to define the .NET SDK version used by your project.
{
"sdk": {
"version": "7.0.100"
}
}
dotnet new sln -n MySolution
dotnet sln add MyApp/MyApp.csproj
dotnet sln add MyLibrary/MyLibrary.csproj
--output <path> Specify output directory
--framework <TFM> Target framework (e.g., net6.0, net7.0)
--configuration Debug or Release
--verbosity Quiet, Minimal, Normal, Detailed
--no-restore Skips implicit restore
The .NET CLI is ideal for automation in continuous integration and delivery systems like GitHub Actions, Azure DevOps, Jenkins, and GitLab CI.
name: Build and Test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: '7.0.x'
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-buildEnsure the .NET SDK path is correctly set in your environment variables.
Check the version with dotnet --info and adjust with global.json.
Use dotnet restore --verbosity detailed to troubleshoot dependency issues.
The .NET CLI is a foundational tool for every .NET developer. It gives you complete control over your development workflow and enables rapid development, automation, and cross-platform compatibility. Whether you're building a console app, a web API, or a cloud-native microservice, mastering the .NET CLI will enhance your productivity and make your development more efficient.
Start using the CLI today and incorporate it into your daily workflow, automation scripts, and DevOps pipelines to take full advantage of the .NET ecosystem.
Copyrights © 2024 letsupdateskills All rights reserved