IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| IConfiguration vs IOptions in .NET | Razor Pages vs MVC vs Minimal APIs in ASP.NET Core | |
β‘ Minimal APIs in .NET Core |
Minimal APIs in .NET Core are a simplified way to build HTTP APIs with minimal code and configuration, introduced in .NET 6. They focus on simplicity, speed, and readability β ideal for small services, microservices, or quick prototypes without the overhead of controllers and MVC.
Program.cs.MapGet, MapPost, etc.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello, Minimal API!");
app.MapPost("/greet", (string name) => $"Hello, {name}!");
app.Run();
This creates two endpoints:
Minimal APIs still use the full ASP.NET Core pipeline. You can add middleware, dependency injection, authentication, and more β just without the extra ceremony of controllers and attributes.
| Feature | Minimal APIs | Controller-Based APIs |
|---|---|---|
| Introduced In | .NET 6 | .NET Core 1.0+ |
| Setup | Defined in Program.cs using MapGet, MapPost |
Uses Controllers, Routing, and Attributes |
| Boilerplate | Minimal code, no controller classes | Requires controller classes and routing attributes |
| Use Case | Microservices, small APIs, prototypes | Large-scale apps, complex business logic |
| Performance | Faster due to lean pipeline | Slightly heavier due to MVC overhead |
| Dependency Injection | Supported via builder.Services |
Supported via constructor injection |
| Validation | Manual or via middleware | Built-in model validation |
| Routing | Fluent routing (e.g., MapGet) |
Attribute-based routing (e.g., [HttpGet]) |
| Swagger Support | Supported with manual configuration | Automatic with annotations |
dotnet new web -n MinimalApiDemo cd MinimalApiDemo
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var items = new List<string>();
// Create
app.MapPost("/items", (string item) => {
items.Add(item);
return Results.Created($"/items/{items.Count - 1}", item);
});
// Read All
app.MapGet("/items", () => items);
// Read by Index
app.MapGet("/items/{id}", (int id) =>
id >= 0 && id < items.Count ? Results.Ok(items[id]) : Results.NotFound()
);
// Update
app.MapPut("/items/{id}", (int id, string newItem) => {
if (id >= 0 && id < items.Count) {
items[id] = newItem;
return Results.Ok(newItem);
}
return Results.NotFound();
});
// Delete
app.MapDelete("/items/{id}", (int id) => {
if (id >= 0 && id < items.Count) {
items.RemoveAt(id);
return Results.NoContent();
}
return Results.NotFound();
});
app.Run();
MapGet, MapPost, MapPut, MapDeletebuilder.Services to add DI or database supportapp.UseSwagger() and app.UseSwaggerUI() for API docsResults for consistent HTTP responsesMinimal APIs are a fast and clean way to build RESTful services in .NET. This example shows how to create a full CRUD app with just a few lines of code.
Minimal APIs are ideal for small, fast, and focused services. Controller-based APIs are better suited for complex applications with layered architecture. You can even mix both in the same project if needed!
| IConfiguration vs IOptions in .NET | Razor Pages vs MVC vs Minimal APIs in ASP.NET Core | |