liveBook · Manning

AddEndpointFilter

As explained in ASP.NET Core in Action, 3e

AddEndpointFilter is an ASP.NET Core endpoint-mapping method that attaches an endpoint filter to an endpoint or route group.

AddEndpointFilter attaches an endpoint filter to a minimal API endpoint or route group. Filters keep cross-cutting code such as validation, logging, authorization, or auditing out of endpoint handlers. section 5.4 section 5.6

Use the filter pipeline to control work before and after an endpoint handler:

  • Receive an EndpointFilterInvocationContext containing details about the selected handler. section 5.4
  • Read handler arguments with context.GetArgument<T>(), using zero for the first argument, one for the second, and so on. section 5.4
  • Return an IResult immediately when validation fails, or call await next(context) to continue through remaining filters and eventually invoke the handler. section 5.4
  • Call AddEndpointFilter multiple times to build a pipeline, or attach one filter to every endpoint in a route group. section 5.6 section 5.5

Extracting ID validation

app.MapGet("/fruit/{id}", (string id) => _fruit.TryGetValue(id, out var fruit) ? TypedResults.Ok(fruit) : Results.Problem(statusCode: 404)) .AddEndpointFilter(ValidationHelper.ValidateId); The filter reads argument 0, returns Results.ValidationProblem for an empty or incorrectly formatted ID, and calls await next(context) for valid input. section 5.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains filter parameters, argument access, short-circuiting, continuation, and pipeline order, while deferring broader validation patterns.
Examples
Demonstrates fruit-ID validation, Results.ValidationProblem, helper methods, multiple filters, and group-level filter application.
What it assumes
Assumes minimal API endpoint mapping and teaches filter contexts, handler arguments, delegates, and continuation from basic behavior.
What you can do after
Build it from scratch by attaching a delegate, reusable method, implementation class, or factory to an endpoint or route group.
How it connects
Connects endpoint filters with minimal APIs, middleware pipelines, route groups, validation, authorization, logging, and auditing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.