liveBook · Manning

Short-circuit evaluation

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

A short circuit is the termination of request-processing pipeline execution by returning a result without invoking the remaining components.

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

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

How deep it goes
Explains continuation, filter order, stage-specific bypass, and earlier-filter callbacks, while deferring some result-filter details.
Examples
Demonstrates invalid identifier validation, a Problem Details response, ordered logging filters, and a minimal API returning fruit data.
What it assumes
Assumes familiarity with request pipelines and filters while introducing short-circuit behavior through endpoint and MVC examples.
What you can do after
Apply it via endpoint or MVC filters by returning a result before invoking the remaining pipeline.
How it connects
Connects endpoint filters with middleware, MVC filters, Razor Pages filters, authorization filters, resource filters, action filters, and result filters.

Also explained in

Compare how 3 books explain short-circuit evaluation →

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.