| OWASP Top 10 :👈 | 👉:Razor Pages vs MVC vs Minimal APIs in ASP.NET Core |
β‘ Minimal APIs in .NET Core |
β‘ What is 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.
π§© What Makes Minimal APIs βMinimalβ?
- No Controllers: Routes and handlers are defined directly in
Program.cs. - No Attributes Needed: Routing uses fluent methods like
MapGet,MapPost, etc. - Top-Level Statements: Uses simplified syntax from C# 9 and .NET 6.
- Lightweight: Perfect for small apps, prototypes, and microservices.
π§ͺ Example
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:
- GET / β returns a simple string.
- POST /greet β accepts a name and returns a greeting.
β Advantages
- Faster development: Less boilerplate, fewer files.
- Better performance: Leaner request pipeline.
- Great for microservices: Small, focused APIs.
- Easy to learn: Perfect for beginners or quick prototypes.
π When to Use
- Small RESTful services or microservices.
- Prototyping or rapid development.
- Serverless functions or lightweight backends.
π« When Not to Use
- Large applications with complex business logic.
- Projects requiring full MVC features (filters, model binding, views).
- Teams that rely on traditional controller-based architecture.
π§ Under the Hood
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.
π Minimal APIs vs Controller-Based APIs in .NET Core
| 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 |
π§± Minimal API CRUD Example in .NET
π¦ Setup
dotnet new web -n MinimalApiDemo
cd MinimalApiDemo
π§© Program.cs
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();
β Features
- Simple routing with
MapGet,MapPost,MapPut,MapDelete - No controllers or attributes
- Perfect for microservices or quick prototypes
π Tips
- Use
builder.Servicesto add DI or database support - Use
app.UseSwagger()andapp.UseSwaggerUI()for API docs - Use
Resultsfor consistent HTTP responses
π Conclusion
Minimal 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.
π Summary
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!
| OWASP Top 10 :👈 | 👉:Razor Pages vs MVC vs Minimal APIs in ASP.NET Core |