CodingNeed.

1 · Structure and rendering · 30 MIN

Minimal APIs and typed route inputs

Endpoint registration connects an HTTP route to application behavior.

A minimal API maps an HTTP method and path to a delegate. Route constraints such as :int prevent non-integer values from matching that route. A successful match still requires a resource lookup and a deliberate missing-resource response. The example exposes only fixed public metadata. Separating the data access behind a service later makes testing easier and prevents route delegates from becoming large mixtures of validation, persistence and presentation.

Inspect the JSON body and status.

Read the example

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/courses/{id:int}", (int id) =>
    id == 1 ? Results.Ok(new {id=1,title="C# foundations"}) : Results.NotFound());
app.Run();
Check the expected output
GET /courses/1 returns 200 and the course. /courses/2 and /courses/text return 404 through different paths.

Your challenge

Add a public list endpoint and distinguish invalid query parameters from a valid-but-missing resource.

Solution cost: O(1) lookup in this fixed example. time · O(1) response data. space

Common trap

Route constraints select routes; they are not a complete domain validation system.

Study the project implementation
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/courses/{id:int}", (int id) =>
    id == 1 ? Results.Ok(new {id=1,title="C# foundations"}) : Results.NotFound());
app.Run();

Further reading: Official documentation

Next lesson: Dependency injection and service lifetimes

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.