ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

ASP.NET Core Tutorial• 7 min read

Modern Minimal APIs in .NET 8

Ultra-fast, low-overhead microservice endpoint routing with lambda expressions

Introduced in .NET 6 and expanded in .NET 8, Minimal APIs are architected for cloud microservices and serverless endpoints that demand maximum performance and minimal boilerplate ceremony.

Why Minimal APIs Excel for Microservices

Near-zero allocation overhead: Bypasses the MVC controller activation pipeline, delivering raw performance close to bare sockets.

Clean lambda mappings: Map HTTP verbs directly on the WebApplication instance: app.MapGet(), app.MapPost(), app.MapDelete().

Route groups: app.MapGroup("/api/v1/courses") allows organizing endpoints with shared middleware, authorization, and OpenAPI tags cleanly.

Modular Route Group in Minimal APIs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Organize endpoints under a unified route group
var coursesGroup = app.MapGroup("/api/courses")
                      .WithTags("Courses API")
                      .RequireAuthorization();

coursesGroup.MapGet("/", async (ApplicationDbContext db) =>
    await db.Courses.AsNoTracking().ToListAsync());

coursesGroup.MapGet("/{id:int}", async (int id, ApplicationDbContext db) =>
    await db.Courses.FindAsync(id) is Course course
        ? Results.Ok(course)
        : Results.NotFound());

coursesGroup.MapPost("/", async (Course newCourse, ApplicationDbContext db) =>
{
    db.Courses.Add(newCourse);
    await db.SaveChangesAsync();
    return Results.Created($"/api/courses/{newCourse.Id}", newCourse);
});

app.Run();
Results.Created and Results.NotFound provide type-safe IResult responses.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which method on WebApplication creates a route group to apply shared route prefixes and authorization policies?