Table of contents
Base Commands
Check .NET Version
Display the currently active .NET SDK version:
dotnet --version
List Installed SDKs
Show all installed .NET SDKs on your machine:
dotnet --list-sdks
List installed runtimes:
dotnet --list-runtimes
List Available Templates
See all available project templates:
dotnet new list
Search for specific templates:
dotnet new list web
Get Help
Get help for any dotnet command:
dotnet --help
dotnet new --help
dotnet build --help
Project Creation
Console Application
dotnet new console -n MyConsoleApp
cd MyConsoleApp
dotnet run
Class Library
dotnet new classlib -n MyLibrary
Specify target framework:
dotnet new classlib -n MyLibrary -f net8.0
Web API
Create a Web API project:
dotnet new webapi -n MyApi
Create API without OpenAPI/Swagger:
dotnet new webapi -n MyApi --no-openapi
Razor Pages App
dotnet new webapp -n MyWebApp
MVC App
dotnet new mvc -n MyMvcApp
Blazor App
Blazor Server:
dotnet new blazorserver -n MyBlazorApp
Blazor WebAssembly:
dotnet new blazorwasm -n MyBlazorApp
Solution Management
Create Solution
Create a new solution file:
dotnet new sln -n MySolution
Or create in current directory:
dotnet new sln
Add Project to Solution
Add a single project:
dotnet sln add MyProject/MyProject.csproj
Add multiple projects:
dotnet sln add MyProject/MyProject.csproj MyLibrary/MyLibrary.csproj
Add all projects in a directory:
dotnet sln add **/*.csproj
Remove Project from Solution
dotnet sln remove MyProject/MyProject.csproj
List Projects in Solution
dotnet sln list
NuGet Packages
Install Package
Install latest version:
dotnet add package Newtonsoft.Json
Install specific version:
dotnet add package Newtonsoft.Json --version 13.0.1
Install prerelease version:
dotnet add package Microsoft.EntityFrameworkCore --prerelease
Note: Run this command in the folder where you have .csproj.
Update Package
Update to latest version:
dotnet add package Newtonsoft.Json
Remove Package
dotnet remove package Newtonsoft.Json
List Packages
List all packages in the project:
dotnet list package
List outdated packages:
dotnet list package --outdated
List packages with vulnerabilities:
dotnet list package --vulnerable
Restore Dependencies
Download and install all NuGet packages:
dotnet restore
When you run it, it:
- Reads your project file (
.csproj) - Looks at
<PackageReference>entries - Downloads missing packages from NuGet or your local cache
- Places them in the global NuGet cache
- Makes the project ready for build
Note: In modern .NET SDK versions, dotnet build and dotnet run automatically perform restore if needed.
Project References
Add Reference
Add reference to another project:
dotnet add reference ../MyLibrary/MyLibrary.csproj
Add multiple references:
dotnet add reference ../LibraryA/LibraryA.csproj ../LibraryB/LibraryB.csproj
Remove Reference
dotnet remove reference ../MyLibrary/MyLibrary.csproj
List References
dotnet list reference
Build & Run
Build Project
Build in Debug mode (default):
dotnet build
Build in Release mode:
dotnet build --configuration Release
Build specific project:
dotnet build MyProject/MyProject.csproj
Build with verbosity:
dotnet build --verbosity detailed
What gets built?
- If there's a
.slnfile → builds the solution - If there's a
.csprojfile → builds that project - Priority:
.sln>.csproj
Clean Build
Remove build outputs:
dotnet clean
Clean and build:
dotnet clean && dotnet build
Rebuild Project
Clean and build in one command:
dotnet build --no-incremental
Run Application
Run the project:
dotnet run
Run with arguments:
dotnet run -- arg1 arg2 arg3
Run specific project:
dotnet run --project MyProject/MyProject.csproj
Run in Release mode:
dotnet run --configuration Release
Run with Profile
Run with specific launch profile from launchSettings.json:
dotnet run --launch-profile http
From outside project directory:
dotnet run --project ./MyWebApp/MyWebApp.csproj --launch-profile http
Watch Mode
Run with hot reload (restarts on file changes):
dotnet watch run
Watch and run tests:
dotnet watch test
Build on file changes:
dotnet watch build
Testing
Create Test Project
Using xUnit (recommended):
dotnet new xunit -n MyProject.Tests
Using NUnit:
dotnet new nunit -n MyProject.Tests
Using MSTest:
dotnet new mstest -n MyProject.Tests
Run Tests
Run all tests:
dotnet test
Run tests with detailed output:
dotnet test --verbosity detailed
Run tests without building:
dotnet test --no-build
Run Specific Tests
Run tests by filter:
dotnet test --filter FullyQualifiedName~MyTest
Run tests from specific class:
dotnet test --filter ClassName=MyTestClass
Run tests by category:
dotnet test --filter Category=Unit
Test with Coverage
Collect code coverage:
dotnet test --collect:"XPlat Code Coverage"
Publishing
Publish Application
Publish for current platform:
dotnet publish -c Release
Publish for specific runtime:
dotnet publish -c Release -r win-x64
dotnet publish -c Release -r linux-x64
dotnet publish -c Release -r osx-x64
Self-Contained Publish
Include .NET runtime (larger size, no runtime dependency):
dotnet publish -c Release -r win-x64 --self-contained true
Framework-dependent (smaller size, requires .NET runtime):
dotnet publish -c Release -r win-x64 --self-contained false
Single File Publish
Publish as single executable:
dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true
Trimmed Publish
Reduce size by removing unused code:
dotnet publish -c Release -r win-x64 --self-contained true -p:PublishTrimmed=true
EF Core
Install EF Core Tools
Install global EF Core CLI tools:
dotnet tool install --global dotnet-ef
Update EF Core tools:
dotnet tool update --global dotnet-ef
Add EF Core packages to project:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Design
Add Migration
dotnet ef migrations add InitialCreate
With output directory:
dotnet ef migrations add InitialCreate --output-dir Data/Migrations
Update Database
Apply migrations to database:
dotnet ef database update
Update to specific migration:
dotnet ef database update MigrationName
Remove Migration
Remove last migration:
dotnet ef migrations remove
Drop Database
dotnet ef database drop
With confirmation:
dotnet ef database drop --force
Troubleshooting
Clear NuGet Cache
Clear all NuGet caches:
dotnet nuget locals all --clear
Clear specific cache:
dotnet nuget locals http-cache --clear
dotnet nuget locals global-packages --clear
Fix Build Errors
Clean and restore:
dotnet clean
dotnet restore
dotnet build
Remove bin and obj folders:
rm -rf **/bin **/obj
dotnet restore
Port Already in Use
Find process using port (Mac/Linux):
lsof -i :5000
kill -9 <PID>
Windows:
netstat -ano | findstr :5000
taskkill /PID <PID> /F
Or change port in launchSettings.json:
{
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
