From b6bfafb11b396518616625ed3a4fb0915bc2c19c Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Wed, 11 May 2022 10:39:47 +0200 Subject: [PATCH] Add MinimalApi features of .NET 7 Preview 4 --- DotNet/ASP.NET/Minimal API.md | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/DotNet/ASP.NET/Minimal API.md b/DotNet/ASP.NET/Minimal API.md index b582366..b05da45 100644 --- a/DotNet/ASP.NET/Minimal API.md +++ b/DotNet/ASP.NET/Minimal API.md @@ -84,6 +84,61 @@ app.MapGet("/search/{id}", Search); IResult Search(int id, int? page = 1, int? pageSize = 10) { /* ... */ } ``` +### Route Groups + +The `MapGroup()` extension method, which helps organize groups of endpoints with a common prefix. +It allows for customizing entire groups of endpoints with a singe call to methods like `RequireAuthorization()` and `WithMetadata()`. + +```cs +var group = app.MapGroup(""); + +group.MapGet("/", GetAllTodos); // route: / +group.MapGet("/{id}", GetTodo); // route: //{id} + +// [...] +``` + +### `TypedResults` + +The `Microsoft.AspNetCore.Http.TypedResults` static class is the “typed” equivalent of the existing `Microsoft.AspNetCore.Http.Results` class. +It's possible to use `TypedResults` in minimal APIs to create instances of the in-framework `IResult`-implementing types and preserve the concrete type information. + +```cs +public static async Task GetAllTodos(TodoDb db) +{ + return TypedResults.Ok(await db.Todos.ToArrayAsync()); +} +``` + +```cs +[Fact] +public async Task GetAllTodos_ReturnsOkOfObjectResult() +{ + // Arrange + var db = CreateDbContext(); + + // Act + var result = await TodosApi.GetAllTodos(db); + + // Assert: Check the returned result type is correct + Assert.IsType>(result); +} +``` + +### Multiple Result Types + +The `Results` generic union types, along with the `TypesResults` class, can be used to declare that a route handler returns multiple `IResult`-implementing concrete types. + +```cs +// Declare that the lambda returns multiple IResult types +app.MapGet("/todos/{id}", async Results, NotFound> (int id, TodoDb db) +{ + return await db.Todos.FindAsync(id) is Todo todo + ? TypedResults.Ok(todo) + : TypedResults.NotFound(); +}); +``` + ## Context With Minimal APIs it's possible to access the contextual information by passing one of the following types as a parameter to your handler delegate: @@ -100,6 +155,21 @@ app.MapGet("/hello", (ClaimsPrincipal user) => { }); ``` +## OpenAPI + +The `Microsoft.AspNetCore.OpenApi` package exposes a `WithOpenApi` extension method that generates an `OpenApiOperation` derived from a given endpoint’s route handler and metadata. + +```cs +app.MapGet("/todos/{id}", (int id) => ...) + .WithOpenApi(); + +app.MapGet("/todos/{id}", (int id) => ...) + .WithOpenApi(operation => { + operation.Summary = "Retrieve a Todo given its ID"; + operation.Parameters[0].AllowEmptyValue = false; + }); +``` + ## Validation Using [Minimal Validation](https://github.com/DamianEdwards/MinimalValidation) by Damian Edwards.