Skip to content

Useful dotnet commands

Common CLI snippets for daily work.

Create a solution and add a project

bash
dotnet new webapi -o MyWebApi

dotnet new sln -o MySolution

cd MySolution

dotnet sln add ../MyWebApi/MyWebApi.csproj

Self-contained publish

bash
dotnet publish ../MyWebApi/MyWebApi.csproj -c Release -r win-x64 --self-contained

List SDKs and runtimes

bash
dotnet --list-sdks

dotnet --list-runtimes

Run

bash
dotnet run --environment Development

dotnet run --urls "http://*:5000;https://localhost:5001"
# localhost:5000 — local only
# 0.0.0.0:5001 — listen on all interfaces (e.g. Ubuntu server)

dotnet run -p ../MyWebApi/MyWebApi.csproj

dotnet run --verbosity detailed

Projects & solutions

bash
dotnet new console -o MyConsoleApp

dotnet new classlib -o MyLibrary

dotnet new webapi -o MyWebApi

dotnet new blazorserver -o MyBlazorApp

dotnet new sln -o MySolution

dotnet sln add MyProject.csproj

dotnet sln remove MyProject.csproj

dotnet sln list

Build & run

bash
dotnet restore

dotnet build

dotnet build --configuration Release

dotnet run

Publish

bash
dotnet publish -r win-x64 --self-contained

dotnet publish -r win-x64 --framework-dependent

Tests & packages

bash
dotnet test

dotnet test --filter MyTest

dotnet list package

dotnet list package --include-transitive

dotnet add package Newtonsoft.Json

dotnet remove package Newtonsoft.Json

dotnet add reference ../MyLib/MyLib.csproj

SDK & environment

bash
dotnet --info

dotnet --list-sdks

dotnet --list-runtimes

dotnet help

dotnet --sdk-version 6.0.100 build

NuGet

bash
dotnet restore

dotnet nuget locals all --clear

dotnet pack

dotnet nuget push MyPackage.nupkg --source https://api.nuget.org/v3/index.json

Misc

bash
dotnet msbuild /t:CustomTarget

dotnet build --verbosity detailed

dotnet interactive

dotnet build --performance-summary

dotnet tool run <tool-name>

dotnet run --sourceLink

Debug / perf

bash
dotnet run --aot

dotnet run --collect:Memory

dotnet publish -r win-x64 --self-contained -p:PublishSingleFile=true
dotnet run --no-build --framework net6.0