Difference between revisions of "Minimal API"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:ASP.NET Category:Core == What is Minimal API == Instead of creating controllers and classes, you define endpoints inline in your app startup (usually Program...")
 
(No difference)

Latest revision as of 09:04, 5 May 2026


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;
});