Skip to content

.NET CLI — Command Line Complete Guide

DodaTech Updated 2026-06-20 7 min read

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
â„šī¸ Info

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

  1. dotnet command not found: The .NET SDK is not installed or not in PATH. Run export PATH="$PATH:$HOME/.dotnet" or install the SDK from dotnet.microsoft.com.

  2. NU1301: Unable to load the service index for source: NuGet cannot reach the configured source. Check internet connectivity and proxy settings (HTTP_PROXY environment variable).

  3. MSB4019: The imported project was not found: The .csproj targets a framework or SDK not installed. Check dotnet --list-sdks and TargetFramework in .csproj.

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

  5. CS0246: The type or namespace name 'X' could not be found: Missing package reference or using directive. Run dotnet add package X or add using X;.

  6. Build succeeds but publish fails: Typically a version mismatch. Use --no-build with publish after a successful build to avoid rebuilding with different settings.

  7. No executable found matching command dotnet-watch: You need to install dotnet-watch or use dotnet watch run which is built-in since .NET 6.

Practice Questions

  1. What is the difference between dotnet build and dotnet publish?
  2. How do you run only specific tests using the CLI?
  3. What flag creates a self-contained deployment?
  4. How do you check which .NET SDK versions are installed?
  5. What is the purpose of dotnet new?

Answers:

  1. dotnet build compiles the project; dotnet publish builds and copies the output (plus dependencies) to a deployable folder.
  2. Use dotnet test --filter "FullyQualifiedName~SecurityTest" or filter by Category, TestCategory, or custom traits.
  3. --self-contained true with a runtime identifier (-r win-x64).
  4. dotnet --list-sdks shows all installed SDKs; dotnet --list-runtimes shows runtimes.
  5. dotnet new creates 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.

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

Do I need Visual Studio to use .NET CLI?

No. The .NET CLI is completely independent of Visual Studio. You can install only the .NET SDK and use any text editor (VS Code, Vim, Sublime) for development. The CLI handles all build, test, and publish operations.

What is the difference between dotnet run and dotnet watch run?

dotnet run builds and runs the project once. dotnet watch run watches for file changes and automatically restarts the app when code changes, enabling a hot-reload development experience.

How do I deploy a .NET app with CLI to a production server?

Use dotnet publish -c Release -o ./publish to produce deployable files. Copy the publish/ folder to the server. If using framework-dependent deployment, ensure the .NET runtime is installed on the server. For self-contained deployment (recommended for production), the server doesn't need the runtime.

Try It Yourself

▶ Try It YourselfEdit the code and click Run

What's Next

NuGet — Package Management Complete Guide
MSBuild — Build Automation Complete Guide
Azure DevOps — CI/CD Pipelines Complete Guide

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro