TExcelTAD
Home › ASP.NET Core › ASP.NET Core Get Started

ASP.NET Core Get Started

To build ASP.NET Core apps you need the .NET SDK — it contains the compiler, the dotnet command-line tool, the runtime and the ASP.NET Core framework. There is nothing else to install: no separate web server, no IIS.

Install the .NET 9 SDK

Download the SDK from dot.net (Windows, macOS, Linux installers) or use your package manager, for example winget install Microsoft.DotNet.SDK.9 on Windows or brew install --cask dotnet-sdk on macOS. Then check that it works:

bash · try it yourself
$ dotnet --version
$ dotnet --list-runtimes
9.0.306
Microsoft.AspNetCore.App 9.0.10 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 9.0.10 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

The SDK version (9.0.306) and the runtime versions (9.0.10) are numbered separately. Microsoft.AspNetCore.App is the shared framework your web apps run on.

Note: Terminal sessions in this course are simulated. Your exact version numbers and paths will differ — any 9.0.x SDK works.

Choose an editor

Editor Notes
Visual Studio Code + C# Dev Kit extension Free, all platforms, great for this course
Visual Studio 2022 (Windows) Full IDE with designers and profilers
JetBrains Rider Cross-platform IDE, free for non-commercial use

Create your first project

dotnet new creates projects from templates. The web template is the smallest ASP.NET Core app:

bash · try it yourself
$ dotnet new web -o HelloApi
$ cd HelloApi
$ ls
The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Restoring /home/me/HelloApi/HelloApi.csproj:
Restore succeeded.

appsettings.Development.json  appsettings.json  HelloApi.csproj  obj  Program.cs  Properties

Other templates you will meet:

Command Template What you get
dotnet new web ASP.NET Core Empty One endpoint, no extras
dotnet new webapi ASP.NET Core Web API A sample minimal API + OpenAPI document (add --use-controllers for controllers)
dotnet new mvc ASP.NET Core MVC Controllers + Razor views
dotnet new webapp ASP.NET Core Web App Razor Pages
dotnet new blazor Blazor Web App Interactive components in C#
dotnet new worker Worker Service A background service without HTTP

Run it

dotnet run restores packages, compiles the project and starts Kestrel. The template picks a random port in Properties/launchSettings.json; in this course we pass --urls so the port is always 5000:

bash · try it yourself
$ dotnet run --urls http://localhost:5000
Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/me/HelloApi

The app keeps running until you press Ctrl+C. In a second terminal (or a browser) send it a request:

bash · try it yourself
$ curl localhost:5000
Hello World!

Change the code

Open Program.cs and edit the message, or add a second endpoint:

csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.MapGet("/about", () => "My first ASP.NET Core app");

app.Run();

Restarting after every change gets tedious. dotnet watch rebuilds and restarts automatically — and for many edits it applies them without a restart at all (Hot Reload):

bash · try it yourself
$ dotnet watch --urls http://localhost:5000
dotnet watch 🔥 Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload.
  💡 Press "Ctrl + R" to restart.
dotnet watch 🔧 Building...
dotnet watch 🚀 Started
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.

Useful dotnet commands

Command Purpose
dotnet new list Show installed templates
dotnet build Compile without running
dotnet run Build and run
dotnet watch Run and reload on file changes
dotnet add package <Name> Add a NuGet package to the project
dotnet test Run unit tests
dotnet publish -c Release Produce deployable output

Tip: dotnet dev-certs https --trust installs a local development certificate so the https:// URL in launchSettings.json works without browser warnings.

Test yourself

Which command creates the smallest ASP.NET Core project?

  1. dotnet new console
  2. dotnet new web
  3. dotnet new mvc
  4. dotnet create aspnet

What does dotnet watch add compared with dotnet run?

  1. It deploys the app to Azure
  2. It runs the tests first
  3. It rebuilds or hot-reloads the app when files change
  4. It disables logging

How did we make the app listen on port 5000?

  1. By editing the .csproj
  2. With dotnet run --urls http://localhost:5000
  3. Port 5000 is always used
  4. By installing IIS