Minimal API

From Logic Wiki
Jump to: navigation, search


What is Minimal API

Instead of creating controllers and classes, you define endpoints inline in your app startup (usually Program.cs). It’s designed for:

  • Simple APIs
  • Microservices
  • Prototypes
  • High-performance endpoints with minimal overhead

How they’re used

You define routes and handlers directly:

var app = WebApplication.Create();

app.MapGet("/hello", () => "Hello world");

app.MapGet("/products/{id}", (int id) =>
{
    return Results.Ok(new { Id = id, Name = "Sample product" });
});

app.Run();

Key ideas

  • Routes + logic in one place (no controller layer)
  • Uses standard HTTP verbs: MapGet, MapPost, etc.
  • Supports dependency injection directly in parameters
  • Works alongside full MVC if needed

Example with dependency injection

app.MapGet("/time", (ILogger<Program> logger) =>
{
    logger.LogInformation("Time requested");
    return DateTime.Now;
});