Short-circuit evaluation stops request-processing pipeline execution by returning a result before the remaining components run. In endpoint filters, a validation filter can reject an invalid identifier without calling next, so later filters and the endpoint handler never execute. section 5.4.1
Apply short-circuiting with these rules:
- Callers normally invoke next to continue the pipeline; returning a result without calling it ends forward execution. section 5.4.1
- Place validation or other rejecting filters deliberately, because filter order controls which components run. section 5.4.1
- Treat MVC and Razor Pages filters differently: setting context.Result can bypass only the relevant stage, while earlier filters may still receive their executed callback. section 22.2
Rejecting an invalid endpoint identifier
A GET endpoint uses a validation filter followed by a logging filter. For a valid identifier, both filters and the handler run. For an invalid identifier, the validation filter returns a Problem Details response instead of calling next; the logging filter and handler are skipped. section 5.4.1
Listing 5.10 Adding multiple filters to the endpoint filter pipeline
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
var _fruit = new ConcurrentDictionary<string, Fruit>();
app.MapGet("/fruit/{id}", (string id) =>
_fruit.TryGetValue(id, out var fruit)
? TypedResults.Ok(fruit)
: Results.Problem(statusCode: 404))
.AddEndpointFilter(ValidationHelper.ValidateId) #1
.AddEndpointFilter(async (context, next) => #2
{
app.Logger.LogInformation("Executing filter..."); #3
object? result = await next(context); #4
app.Logger.LogInformation($"Handler result: {result}"); #5
return result; #6
});
app.Run();
#1 Adds the validation filter as before
#2 Adds a new filter using a lambda function
#3 Logs a message before executing the rest of the pipeline
#4 Executes the remainder of the pipeline and the endpoint handler
#5 Logs the result returned by the rest of the pipeline
#6 Returns the result unmodified
Browse the minimal API showing validation short-circuiting, filter order, continuation through next, and logging around the handler result. section 5.4.1


