Abstract [ Documentation]

Minimal APIs are a simplified approach to building fast HTTP APIs with ASP.NET Core that skip traditional scaffolding and avoid unnecessary controllers. They fluently declare API routes and actions:

var app = WebApplication.Create(args);

app.MapGet("/", () => "Hello World!");

app.MapGet("/users/{userId}/books/{bookId}", 
    (int userId, int bookId) => $"The user id is {userId} and book id is {bookId}");

app.Run();

Middleware Sequencing in Minimal API Apps [ Documentation]

WebApplication adds the following middleware:

SequenceMiddlewareConditions
1UseDeveloperExceptionPageHostingEnvironment == “Development”
2UseRoutingEndpoints are configured (like with app.MapGet)
3UseEndpointsEndpoints are configured
4UseAuthenticationAddAuthentication is called
5UseAuthorizationAddAuthorization is called

All user-configured middleware and endpoints are added after UseRouting (#2) but before UseEndpoints (#3).

Cross-Origin Resource Sharing Middleware (UseCors)

If the app requires CORS:

  1. Call UseCors manually
  2. Call UseAuthentication manually
  3. Call UseAuthorization manually

Running Middleware Before Rout Matching

If middleware must be run before route matching occurs:

  1. Call app.Use((context, next) => { }) for middleware that needs to run first
  2. Call UseRouting manually

UseEndpoints will be called automatically.

Terminal Middleware

This is middleware that runs if no endpoint handles the request.

When using terminal middleware:

  1. Call UseRouting manually
  2. Call MapGet manually
  3. Call UseEndpoints manually
  4. Call the terminal middleware:
    app.Run(context => 
    {
        context.Response.StatusCode = 404;
        return Task.CompletedTask;
    });