From 82642f25bc72c004ddb3be23238f6dedd5950285 Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Fri, 13 May 2022 19:50:12 +0200 Subject: [PATCH] Add minimal api filter notes --- dotnet/asp.net/minimal-api.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dotnet/asp.net/minimal-api.md b/dotnet/asp.net/minimal-api.md index b05da45..07d6143 100644 --- a/dotnet/asp.net/minimal-api.md +++ b/dotnet/asp.net/minimal-api.md @@ -139,6 +139,25 @@ app.MapGet("/todos/{id}", async Results, NotFound> (int id, TodoDb db) }); ``` +## Filters + +```cs +public class ExampleFilter : IRouteHandlerFilter +{ + public async ValueTask InvokeAsync(RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) + { + // before endpoint call + var result = next(context); + /// after endpoint call + return result; + } +} +``` + +```cs +app.MapPost("/route", Handler).AddFilter(); +``` + ## 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: