The dotnet CLI
🎯 What You'll Learn
- Understanding the dotnet command structure
- Essential commands for daily development
- Creating projects and solutions
- Building, running, and testing applications
- Publishing and deployment commands
- Working with templates
- Useful flags and options
- Productivity tips and shortcuts
What is the dotnet CLI?
The dotnet CLI (Command-Line Interface) is your primary tool for working with .NET. It's a cross-platform command-line tool that handles everything from creating projects to deploying applications.
Universal: Works the same on Windows, macOS, and Linux
Scriptable: Automate builds and deployments
Fast: Quicker than clicking through menus
CI/CD Ready: Perfect for automated pipelines
IDE Independent: Works without Visual Studio
Command Structure
All dotnet commands follow this pattern:
dotnet [command] [arguments] [options]
For example:
dotnet new console -n MyApp -o ./src/MyApp
dotnet- The CLI toolnew- Command (create new project)console- Argument (template name)-n MyApp- Option (name)-o ./src/MyApp- Option (output directory)
Essential Commands
Getting Help
# General help
dotnet --help
dotnet -h
# Help for specific command
dotnet new --help
dotnet build --help
# List all available commands
dotnet --list-sdks
dotnet --list-runtimes
Version Information
# Show .NET version
dotnet --version
# Show detailed .NET info
dotnet --info
Project Creation Commands
dotnet new - Create Projects
# List all available templates
dotnet new list
# Create console application
dotnet new console -n MyConsoleApp
# Create ASP.NET Core Web API
dotnet new webapi -n InvenTrack.Api
# Create class library
dotnet new classlib -n InvenTrack.Core
# Create solution file
dotnet new sln -n InvenTrack
# Create .gitignore
dotnet new gitignore
# Create with specific framework
dotnet new console -n MyApp -f net8.0
Common Templates
| Template | Short Name | Description |
|---|---|---|
| Console Application | console |
Command-line application |
| Class Library | classlib |
Reusable library (DLL) |
| ASP.NET Core Web API | webapi |
RESTful API |
| ASP.NET Core MVC | mvc |
Model-View-Controller web app |
| Razor Pages | webapp |
Page-focused web app |
| Blazor Server | blazorserver |
Server-side Blazor app |
| Worker Service | worker |
Background service |
| xUnit Test Project | xunit |
Unit test project |
-n, --name: Project name
-o, --output: Output directory
-f, --framework: Target framework (net8.0, net7.0, etc.)
--no-restore: Skip automatic package restore
Solution Management
dotnet sln - Manage Solutions
# Create new solution
dotnet new sln -n InvenTrack
# Add project to solution
dotnet sln add src/InvenTrack.Web/InvenTrack.Web.csproj
dotnet sln add src/InvenTrack.Core/InvenTrack.Core.csproj
# Add multiple projects
dotnet sln add src/**/*.csproj
# Remove project from solution
dotnet sln remove src/InvenTrack.Web/InvenTrack.Web.csproj
# List projects in solution
dotnet sln list
dotnet add reference - Project References
# Add project reference
dotnet add src/InvenTrack.Web reference src/InvenTrack.Core
# Add multiple references
dotnet add reference ../InvenTrack.Core ../InvenTrack.Data
# Remove reference
dotnet remove reference ../InvenTrack.Core
Build and Run Commands
dotnet restore - Restore Dependencies
# Restore NuGet packages
dotnet restore
# Restore for specific project
dotnet restore src/InvenTrack.Web/InvenTrack.Web.csproj
# Force re-download of packages
dotnet restore --force
You rarely need to run dotnet restore manually. It's automatically run by:
• dotnet build
• dotnet run
• dotnet test
Only needed after cloning a repo or modifying package references.
dotnet build - Compile Projects
# Build current project/solution
dotnet build
# Build specific project
dotnet build src/InvenTrack.Web/InvenTrack.Web.csproj
# Build in Release mode
dotnet build --configuration Release
dotnet build -c Release
# Build without restoring
dotnet build --no-restore
# Build with verbose output
dotnet build --verbosity detailed
dotnet build -v d
dotnet run - Build and Execute
# Run current project
dotnet run
# Run specific project
dotnet run --project src/InvenTrack.Web
# Run in Release mode
dotnet run --configuration Release
# Pass arguments to your app
dotnet run -- arg1 arg2
# Run without building (if already built)
dotnet run --no-build
dotnet watch - Hot Reload
# Run with hot reload (auto-restart on file changes)
dotnet watch run
# Watch and run tests
dotnet watch test
# Watch specific project
dotnet watch --project src/InvenTrack.Web run
Use dotnet watch run during development! It automatically rebuilds and
restarts your app whenever you save changes. Perfect for rapid iteration.
dotnet clean - Remove Build Artifacts
# Clean build output (bin/ and obj/)
dotnet clean
# Clean specific configuration
dotnet clean --configuration Release
Testing Commands
dotnet test - Run Tests
# Run all tests
dotnet test
# Run tests in specific project
dotnet test tests/InvenTrack.Tests/InvenTrack.Tests.csproj
# Run tests with detailed output
dotnet test --verbosity normal
# Run tests and collect code coverage
dotnet test --collect:"XPlat Code Coverage"
# Run specific test
dotnet test --filter "FullyQualifiedName~InvenTrack.Tests.ProductTests"
# Run tests in parallel
dotnet test --parallel
Publishing and Deployment
dotnet publish - Prepare for Deployment
# Publish for deployment
dotnet publish
# Publish in Release mode
dotnet publish -c Release
# Publish to specific folder
dotnet publish -c Release -o ./publish
# Publish self-contained (includes .NET runtime)
dotnet publish -c Release --self-contained -r win-x64
# Publish framework-dependent (requires .NET runtime on server)
dotnet publish -c Release --no-self-contained
# Publish single file
dotnet publish -c Release -r win-x64 --self-contained -p:PublishSingleFile=true
Runtime Identifiers (RID)
| Platform | RID |
|---|---|
| Windows x64 | win-x64 |
| Windows ARM64 | win-arm64 |
| Linux x64 | linux-x64 |
| Linux ARM64 | linux-arm64 |
| macOS x64 | osx-x64 |
| macOS ARM64 | osx-arm64 |
Self-Contained: Includes .NET runtime (larger, but no dependencies)
Framework-Dependent: Requires .NET runtime on target (smaller)
For InvenTrack: Use framework-dependent for servers with .NET installed
Package Management
dotnet add package - Add NuGet Packages
# Add latest version
dotnet add package Newtonsoft.Json
# Add specific version
dotnet add package Serilog --version 3.1.1
# Add to specific project
dotnet add src/InvenTrack.Web package Microsoft.EntityFrameworkCore
# Add prerelease version
dotnet add package SomePackage --prerelease
dotnet remove package - Remove Packages
# Remove package
dotnet remove package Newtonsoft.Json
# Remove from specific project
dotnet remove src/InvenTrack.Web package Serilog
dotnet list package - View Packages
# List all packages
dotnet list package
# Check for outdated packages
dotnet list package --outdated
# Check for vulnerable packages
dotnet list package --vulnerable
Tool Management
dotnet tool - Global and Local Tools
# Install global tool
dotnet tool install --global dotnet-ef
# Install local tool (project-specific)
dotnet tool install dotnet-ef
# List installed global tools
dotnet tool list --global
# Update tool
dotnet tool update --global dotnet-ef
# Uninstall tool
dotnet tool uninstall --global dotnet-ef
Popular .NET Tools
| Tool | Purpose |
|---|---|
dotnet-ef |
Entity Framework Core migrations |
dotnet-aspnet-codegenerator |
ASP.NET Core scaffolding |
dotnet-format |
Code formatter |
dotnet-outdated |
Check for outdated packages |
Useful Flags and Options
Common Flags Across Commands
| Flag | Description |
|---|---|
-h, --help |
Show help for command |
-c, --configuration |
Build configuration (Debug/Release) |
-f, --framework |
Target framework |
-o, --output |
Output directory |
-v, --verbosity |
Output verbosity (q/m/n/d/diag) |
--no-restore |
Skip package restore |
--no-build |
Skip build step |
Real-World InvenTrack Workflow
Here's a complete workflow for setting up InvenTrack from scratch:
# 1. Create solution
dotnet new sln -n InvenTrack
# 2. Create projects
dotnet new webapi -n InvenTrack.Api -o src/InvenTrack.Api
dotnet new classlib -n InvenTrack.Core -o src/InvenTrack.Core
dotnet new classlib -n InvenTrack.Data -o src/InvenTrack.Data
dotnet new xunit -n InvenTrack.Tests -o tests/InvenTrack.Tests
# 3. Add projects to solution
dotnet sln add src/InvenTrack.Api
dotnet sln add src/InvenTrack.Core
dotnet sln add src/InvenTrack.Data
dotnet sln add tests/InvenTrack.Tests
# 4. Add project references
dotnet add src/InvenTrack.Api reference src/InvenTrack.Core
dotnet add src/InvenTrack.Core reference src/InvenTrack.Data
dotnet add tests/InvenTrack.Tests reference src/InvenTrack.Core
# 5. Add NuGet packages
dotnet add src/InvenTrack.Data package Microsoft.EntityFrameworkCore
dotnet add src/InvenTrack.Data package Microsoft.EntityFrameworkCore.SqlServer
dotnet add src/InvenTrack.Api package Serilog.AspNetCore
# 6. Build everything
dotnet build
# 7. Run the API
dotnet run --project src/InvenTrack.Api
# 8. Run tests
dotnet test
Productivity Tips
1. Use Aliases (PowerShell/Bash)
# Add to $PROFILE
function dr { dotnet run }
function db { dotnet build }
function dt { dotnet test }
function dw { dotnet watch run }
2. Tab Completion
The dotnet CLI supports tab completion. Type dotnet new and press Tab to
cycle through available templates.
3. Combine Commands
# Clean, build, and run
dotnet clean && dotnet build && dotnet run
# Build and test
dotnet build && dotnet test
Key Takeaways
- The dotnet CLI is your primary tool for .NET development
dotnet newcreates projects from templatesdotnet slnmanages multi-project solutionsdotnet buildcompiles your codedotnet runbuilds and executes your appdotnet watch runenables hot reload for rapid developmentdotnet testruns your unit testsdotnet publishprepares apps for deploymentdotnet add packageinstalls NuGet packagesdotnet toolmanages global and local tools- Use
--helpon any command to see all options - Commands are cross-platform and work identically everywhere
You're now proficient with the dotnet CLI! In the final section of Part II, we'll dive into NuGet Package Management—how to find, install, and manage third-party libraries to supercharge your InvenTrack application.