As .NET 6 was released in November 2021, one of the most exciting additions wasΒ Minimal APIsΒ β a fresh, streamlined approach to building HTTP APIs in ASP.NET Core. Whether you’re crafting microservices, building a prototype, or just tired of boilerplate, Minimal APIs offer a much simpler way to get started.
In this post, weβll look at what Minimal APIs are, how they compare to traditional Controllers, and walk through a simple example.
π§© What Are Minimal APIs?
Minimal APIs allow developers to define route handlers directly in the Program.cs
file, without the need for controllers, attributes, or extra layers of abstraction. They are part of ASP.NET Core’s evolution toward more expressive and flexible app-building experiences.
They aim to reduce ceremony, especially for small services where full-blown MVC might feel overkill.
π Minimal API vs. Controller Example
Hereβs a quick comparison between the traditional controller-based API and a Minimal API approach:
β Traditional Controller-Based
// ProductsController.cs
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult Get() => Ok(new[] { "Apple", "Banana", "Orange" });
}
β Minimal API
// Program.cs in .NET 6
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/products", () =>
{
return new[] { "Apple", "Banana", "Orange" };
});
app.Run();
Notice how the Minimal API removes the need for routing attributes, a controller class, and even method-level annotations.
π οΈ Real-World Example: In-Memory CRUD
Letβs expand this to a simple in-memory CRUD example for products:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var products = new List<string> { "Apple", "Banana" };
app.MapGet("/products", () => products);
app.MapPost("/products", (string product) =>
{
products.Add(product);
return Results.Created($"/products/{product}", product);
});
app.MapDelete("/products/{name}", (string name) =>
{
products.Remove(name);
return Results.Ok();
});
app.Run();
This is a functional CRUD API β in less than 20 lines.
βοΈ When to Use Minimal APIs
Minimal APIs are a great fit for:
β Microservices β Rapid prototypes or internal tools β Apps with simple endpoints and lightweight needs β Developers coming from Node.js/Express who prefer flat route handlers
But there are situations where traditional Controllers might still be better:
π If you need extensive model validation with data annotations π§± If you rely on filters, conventions, or controller-specific features π§ͺ If your team is heavily invested in existing MVC architecture
π± What Comes Next?
.NET 7 and 8 continued to improve the Minimal API experience by adding better support for filters, route grouping, and OpenAPI integration.
So, if you’re starting fresh or just want a clean, modern way to build lightweight services β Minimal APIs are absolutely worth trying.
π¬ What Do You Think?
Have you started using Minimal APIs in your projects? Let me know your thoughts and how it compares to your experience with traditional ASP.NET Controllers!
Let me know if you’d like a follow-up post β for example:
- Advanced Minimal API patterns
- Using dependency injection
- Adding authentication & authorization
- OpenAPI/Swagger integration
Happy coding!