0Pricing
C# Academy · Lesson

Short-Circuiting & Branching

Use MapWhen, UseWhen, and terminal middleware to branch or terminate the pipeline based on request conditions.

Branching and Short-Circuiting

Not every request should traverse the full middleware pipeline. ASP.NET Core provides tools to branch (route subsets to different pipelines) and short-circuit (stop processing and return immediately).

Map: Permanent Branch by Path

Map creates a permanent branch — requests matching the prefix go into the branch and NEVER return to the main pipeline.

app.Map("/api", apiApp =>
{
    apiApp.UseAuthentication();
    apiApp.UseAuthorization();
    apiApp.Run(async ctx =>
        await ctx.Response.WriteAsync("API branch"));
});

// Requests to /api/... enter the branch above.
// Requests to /public/... skip the branch entirely.
app.Run(async ctx =>
    await ctx.Response.WriteAsync("Main pipeline"));

All lessons in this course

  1. ASP.NET Core Pipeline Overview
  2. Writing Custom Middleware
  3. Short-Circuiting & Branching
  4. Middleware Ordering & Built-in Middleware
← Back to C# Academy