Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
C# and .NET Basics  ›  Lesson

The .NET CLI — One Command for Everything

.NET CLI 12 minThe dotnet command creates, restores, builds, runs, tests and publishes projects, so every task is a verb after the word dotnet.

The .NET CLI — One Command for Everything

You do not need a heavy IDE to build ReadingRoom. Every task — create a project, add a package, build, run, test, publish — is one command in a terminal. The same commands run on your laptop and in a CI pipeline, which is exactly why they are worth learning.

The rule

dotnet plus a verb: new, add, build, run, test, publish.

The command is always dotnet <verb> [options]. The verbs you will type most:

dotnet new webapi -n ReadingRoom.Api   # create a project from a template
dotnet add package Dapper              # add a NuGet package
dotnet build                           # compile (restores packages first)
dotnet run                             # build and start the app
dotnet watch                           # run and reload on every save
dotnet test                            # build and run the tests
dotnet publish -c Release              # produce files ready to deploy

dotnet new list shows the templates: web (an empty site), webapi, mvc, razor (Razor Pages), blazor, classlib, xunit and more. A solution (.sln) groups several projects so one command builds them all.

Packages come from NuGet, .NET's package registry. dotnet add package writes a <PackageReference> into the .csproj; the next build downloads it. You rarely type dotnet restore yourself — build, run and test restore first.

The shortcut

Say what you want in plain words, then find the verb: make something → new; bring in something → add; check it compiles → build; see it working → run; prove it works → test; ship it → publish.

Setting up ReadingRoomStep 1 of 5
  1. Make the solution

    dotnet new sln -n ReadingRoom creates the file that holds the projects.

The trap: running dotnet run in a folder with no project, or with a solution but no --project. The CLI needs to know which project to start.

Drills

Answer fast — trust the rule.

quick drill

Which command adds the Dapper package to the current project?

quick drill

You want the API to restart every time you save a file. Which command?

quick drill

Where does dotnet add package record the dependency?

quick drill

Which template gives you an empty web app with almost nothing in it?

Remember

  • dotnet plus a verb: new, add, build, run, test, publish.
  • Say the goal in plain words, then pick the verb.
  • Build, run and test restore packages for you.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFrom nothing to a running API

List the dotnet commands you would type to create a ReadingRoom solution with an API project and a test project, add a NuGet package, and run the tests. Explain what each command does.