.NET CLI â Command Line Complete Guide
In this tutorial, you'll learn about .net cli. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The .NET CLI (Command-Line Interface) is a cross-platform toolchain for developing, building, running, and publishing .NET applications from the terminal, providing all the functionality of Visual Studio without the GUI overhead.
What You'll Learn
You'll master essential .NET CLI commands â creating projects, restoring packages, building, testing, publishing, managing NuGet packages, running diagnostics, and integrating with CI/CD pipelines.
Why the .NET CLI Matters
Real-world .NET development happens on the command line â in CI/CD pipelines, Docker containers, and remote servers. Every .NET developer must be comfortable with the CLI. DodaTech uses the CLI in all build pipelines for Doda Browser, DodaZIP, and Durga Antivirus Pro to ensure reproducible builds across platforms.
Real-World Use
A CI/CD pipeline in Azure DevOps runs dotnet build, dotnet test, dotnet pack, and dotnet nuget push â all without Visual Studio. The same commands a developer runs locally run in production CI, ensuring consistency.
.NET CLI Learning Path
flowchart LR A["Terminal Basics"] --> B[".NET CLI Overview"] B --> C["Project Management"] C --> D["Build, Test, Publish"] D --> E["NuGet & Tools"] E --> F["CI/CD Integration"] C:::current classDef current fill:#f90,color:#fff,stroke:#333,stroke-width:2px
Prerequisites: Install the .NET SDK 6+ from dotnet.microsoft.com. Basic terminal familiarity (PowerShell, bash, or zsh).
Essential Commands Reference
| Command | What It Does | Common Flags |
|---|---|---|
dotnet new |
Create a new project from a template | -n name, -o output, -f framework |
dotnet restore |
Download NuGet packages | --source custom feed |
dotnet build |
Compile the project | -c configuration, -o output |
dotnet test |
Run unit tests | --filter test filter, --collect Code Coverage |
dotnet run |
Build and run | --project specify project file |
dotnet publish |
Produce deployable output | -c, -o, --self-contained |
dotnet pack |
Create NuGet package | -c, -o |
dotnet tool |
Manage global/local tools | install, list, update |
dotnet format |
Apply code style | --verify-no-changes for CI |
Code Examples
Example 1: Complete Project Lifecycle
# Create a new web API project
dotnet new webapi -n DodaTech.Api -o ./DodaTech.Api
# Navigate and restore (restore happens automatically for SDK projects)
cd DodaTech.Api
# Build in Release mode
dotnet build -c Release
# Run the project
dotnet run --urls "https://localhost:5001"
# Run in watch mode (hot reload)
dotnet watch run
Example 2: Testing with Filtering
# Run all tests
dotnet test
# Run tests matching a category
dotnet test --filter "Category=Security"
# Run tests with code coverage
dotnet test --collect "XPlat Code Coverage" \
--settings coverlet.runsettings
# Generate coverage report
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:"**/coverage.cobertura.xml" \
-targetdir:"CoverageReport" -reporttypes:Html
Expected output:
Test run for /home/user/DodaTech.Api/tests/DodaTech.Tests/bin/Release/net8.0/DodaTech.Tests.dll
Passed! - Failed: 0, Passed: 142, Skipped: 3, Total: 145, Duration: 12s
Example 3: Publishing for Different Platforms
# Framework-dependent deployment (requires runtime on target)
dotnet publish -c Release -o ./publish
# Self-contained deployment (includes runtime)
dotnet publish -c Release -o ./publish \
--self-contained true \
-r win-x64
# Single-file executable
dotnet publish -c Release -o ./publish \
--self-contained true \
-r linux-x64 \
-p:PublishSingleFile=true \
-p:IncludeNativeLibrariesForSelfExtract=true
# Trim to reduce size
dotnet publish -c Release -o ./publish \
-r linux-x64 \
-p:PublishTrimmed=true \
-p:TrimMode=Link
Expected output:
MSBuild version 17.8.3+...
DodaTech.Api -> /home/user/DodaTech.Api/publish/
Published to /home/user/DodaTech.Api/publish/
Example 4: Custom Project Template
Create reusable project templates for your team:
# Create a template from an existing project
dotnet new template DodaTech.SecurityScan \
--template-name "DodaTech Security Scanner" \
--identity DodaTech.SecurityScan.Template
# Install the template
dotnet new install ./DodaTech.SecurityScan
# Create a new project from the template
dotnet new dodatech-security -n MySecurityScan
.NET SDK Diagnostics
# Check installed SDKs and runtimes
dotnet --list-sdks
dotnet --list-runtimes
# Find which SDK version a project uses
dotnet --version
# Diagnose build issues
dotnet build -v diag
# Check for outdated NuGet packages
dotnet list package --outdated
# View dependency tree
dotnet list package --include-transitive
Global and Local Tools
# Install a global tool
dotnet tool install -g dotnet-ef # Entity Framework CLI
dotnet tool install -g dotnet-script # Run C# scripts
dotnet tool install -g dotnet-format # Code formatter
# Install a local tool (project-scoped)
dotnet new tool-manifest
dotnet tool install dotnet-ef
dotnet tool restore
# Run a local tool
dotnet ef migrations add InitialCreate
CI/CD Integration
GitHub Actions Example
name: .NET Build and Test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x
- name: Restore
run: dotnet restore
- name: Build
run: dotnet build -c Release --no-restore
- name: Test
run: dotnet test -c Release --no-build --collect "XPlat Code Coverage"
Common Errors
dotnetcommand not found: The .NET SDK is not installed or not in PATH. Runexport PATH="$PATH:$HOME/.dotnet"or install the SDK from dotnet.microsoft.com.NU1301: Unable to load the service index for source: NuGet cannot reach the configured source. Check internet connectivity and proxy settings (
HTTP_PROXYenvironment variable).MSB4019: The imported project was not found: The
.csprojtargets a framework or SDK not installed. Checkdotnet --list-sdksandTargetFrameworkin.csproj.NETSDK1045: The current .NET SDK does not support targeting .NET 8.0: You need a newer SDK. Install the SDK matching your target framework version.
CS0246: The type or namespace name 'X' could not be found: Missing package reference or
usingdirective. Rundotnet add package Xor addusing X;.Build succeeds but publish fails: Typically a version mismatch. Use
--no-buildwith publish after a successful build to avoid rebuilding with different settings.No executable found matching command dotnet-watch: You need to install
dotnet-watchor usedotnet watch runwhich is built-in since .NET 6.
Practice Questions
- What is the difference between
dotnet buildanddotnet publish? - How do you run only specific tests using the CLI?
- What flag creates a self-contained deployment?
- How do you check which .NET SDK versions are installed?
- What is the purpose of
dotnet new?
Answers:
dotnet buildcompiles the project;dotnet publishbuilds and copies the output (plus dependencies) to a deployable folder.- Use
dotnet test --filter "FullyQualifiedName~SecurityTest"or filter byCategory,TestCategory, or custom traits. --self-contained truewith a runtime identifier (-r win-x64).dotnet --list-sdksshows all installed SDKs;dotnet --list-runtimesshows runtimes.dotnet newcreates a new project, solution, or file from a template (e.g.,dotnet new webapi,dotnet new classlib).
Challenge
Create a bash script that builds a .NET solution, runs all tests, measures Code Coverage, generates a coverage report, packages each project as a NuGet package, and publishes to a local feed â all in one command.
Real-World Task
Set up a GitHub Actions workflow for a .NET solution that: builds on three OS (ubuntu, windows, macos), runs tests with Code Coverage, packs NuGet packages, and publishes them to GitHub Packages â with a matrix strategy for .NET 6, 7, and 8.
Featured Snippet
What is the .NET CLI?
The .NET CLI is a cross-platform command-line toolchain for creating, building, testing, and publishing .NET applications, providing Visual Studio-equivalent functionality for terminal-based development and CI/CD pipelines.
FAQ
Try It Yourself
What's Next
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro