100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

C# Minimal APIs Cheat Sheet

C# Minimal APIs Cheat Sheet

Covers ASP.NET Core minimal API setup, routing, model binding, dependency injection, validation, and OpenAPI integration for .NET 8/9.

2 PagesIntermediateFeb 18, 2026

Minimal API Bootstrap

The full `Program.cs` for a minimal API app — no Startup.cs needed.

csharp
var builder = WebApplication.CreateBuilder(args);builder.Services.AddEndpointsApiExplorer();builder.Services.AddOpenApi(); // .NET 9 built-in OpenAPIvar app = builder.Build();app.MapGet("/", () => "Hello, World!");app.MapGet("/items/{id:int}", (int id) => Results.Ok(new { id }));app.MapOpenApi();app.Run();

Routes, Route Groups & Verbs

Group related endpoints and share prefixes/filters/tags.

csharp
var items = app.MapGroup("/items").WithTags("Items");items.MapGet("/", () => Results.Ok(itemStore));items.MapGet("/{id:guid}", (Guid id) =>    itemStore.TryGetValue(id, out var item)        ? Results.Ok(item)        : Results.NotFound());items.MapPost("/", (CreateItemDto dto, IItemService svc) =>{    var created = svc.Create(dto);    return Results.Created($"/items/{created.Id}", created);});items.MapDelete("/{id:guid}", (Guid id, IItemService svc) =>    svc.Delete(id) ? Results.NoContent() : Results.NotFound());

Dependency Injection & Validation

Handlers pull services straight from DI by parameter type; use filters for validation.

csharp
builder.Services.AddScoped<IItemService, ItemService>();builder.Services.AddValidation(); // .NET 8+ DataAnnotations validationapp.MapPost("/items", (CreateItemDto dto, IItemService svc) =>{    if (!MiniValidator.TryValidate(dto, out var errors))        return Results.ValidationProblem(errors);    return Results.Ok(svc.Create(dto));}).AddEndpointFilter(async (ctx, next) =>{    // custom endpoint filter, e.g. logging/auth checks    return await next(ctx);});

`Results` Helper Methods

Common typed responses returned from handler delegates.

  • Results.Ok(obj)- 200 with JSON body
  • Results.Created(uri, obj)- 201 with Location header
  • Results.NoContent()- 204 with empty body
  • Results.NotFound()- 404
  • Results.BadRequest(obj)- 400 with error payload
  • Results.ValidationProblem(errors)- 400 in RFC 7807 problem-details shape
  • TypedResults.Ok(obj)- strongly-typed variant, improves OpenAPI schema generation
Pro Tip

Prefer the `TypedResults` static class over `Results` in handler return types — it gives you compile-time-checked return types AND automatically produces accurate OpenAPI response schemas, which plain `Results` (returning IResult) cannot.

Was this cheat sheet helpful?

Explore Topics

#CMinimalAPIs#CMinimalAPIsCheatSheet#Programming#Intermediate#MinimalAPIBootstrap#Routes#Route#Groups#APIs#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet