Using ASP.NET 6 Minimal APIs
Posted: (EET/GMT+2)
ASP.NET Core 6 introduced Minimal APIs as a lighter way to build HTTP APIs without controllers, attributes, or the usual MVC structure.
They are useful for small services, internal tools, health checks, and simple API backends. For production use, it is still worth keeping a few basics in place.
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplore();
builder.Services.AddSwaggerGen();
WebApplication app = builder.Build();
if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/health", () => Results.Ok("OK"));
app.MapGet("/version", () => {
return Results.Ok(new {
Application = "SampleApi", Version = "1.0.0" });
});
app.Run();
This keeps the application small, but still gives you useful defaults: environment-specific Swagger, simple health check endpoint, and a version endpoint for quick diagnostics.
Your route handlers should be short (and fast). If the logic grows, move it into a separate service class and inject that service into the endpoint.
Here's another example about route constraints:
app.MapGet("/customers/{id:int}",
async (int id, CustomerService service) => {
Customer? customer = await service.GetCustomerAsync(id);
return customer is null ?
Results.NotFound() : Results.Ok(customer);
});
Also add logging and configuration the same way you would in a regular ASP.NET Core app. Minimal APIs remove ceremony, not production requirements.
Good production defaults include clear routes, typed responses, validation, logging, health checks, and environment-specific configuration.