.NET APIs · Lesson 1

Your first minimal API

Today’s win: understand the smallest useful ASP.NET Core API and run it from the terminal on Linux or macOS. No Visual Studio, no Windows-only assumptions.

The mental model

A .NET API is just a program that starts a web server, maps HTTP routes to C# functions, and waits for requests. ASP.NET Core’s Minimal APIs let you express that directly in Program.cs.

Think in three steps

  1. Build an app: create the web application object.
  2. Map endpoints: say which URL and HTTP method call which function.
  3. Run the app: start listening for HTTP requests.

The tiny version

var app = WebApplication.Create(args);

app.MapGet("/", () => "Hello World!");

app.Run();

Your terminal path

On Linux and macOS, the official path is the dotnet CLI. The CLI comes with the .NET SDK and works the same way across both systems.

If dotnet --info fails, install the .NET SDK first. Prefer the official Microsoft package instructions for your Linux distro or macOS installer/Homebrew route.

Open Program.cs

For a fresh dotnet new web app, expect something close to this:

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

app.MapGet("/", () => "Hello World!");

app.Run();

Read it literally: create a builder, build an app, map GET / to a function, run the web server. The lambda () => "Hello World!" is the request handler.

Make it an API, not just hello world

Replace the route with a small JSON response:

app.MapGet("/status", () => new
{
    Service = "HelloApi",
    Status = "ok",
    CheckedAt = DateTimeOffset.UtcNow
});

Then run again and call it:

dotnet run
curl http://localhost:5000/status

If the console says it is listening on a different port, use that port. On modern templates it may choose a random dev port like http://localhost:5123.

Retrieval checkpoint — reply in Discord

Before the next lesson, answer these from memory:

  1. In your own words, what does app.MapGet("/status", ...) do?
  2. What is the difference between the .NET SDK and the .NET runtime?
  3. Paste the JSON you get from your /status endpoint, or paste the error if it fails.

Primary source

Read Microsoft Learn’s Minimal APIs overview and .NET CLI overview when you want the official reference:Minimal APIsand .NET CLI.