In ASP.NET Core 10, what do app.MapShortCircuit(404, "robots.txt", "wp-admin") and an endpoint's .ShortCircuit() do, and which middleware do they skip?
answer
- the endpoint runs inside UseRouting
- everything after UseRouting is skipped
- ShortCircuit(int? statusCode = null)
- {prefix}/{**catchall}, Order int.MaxValue
- auth, CORS or antiforgery metadata: InvalidOperationException
basics
~20 sShortCircuit() makes the routing middleware run a matched endpoint immediately and end the request, skipping all middleware after UseRouting. MapShortCircuit maps prefixes to an empty short-circuit endpoint returning a fixed status code, cheaply dropping bot probes.
solid answer
~40 sNormally `UseRouting` only selects an endpoint, and middleware between it and the endpoint (authentication, authorization, CORS, rate limiting, request timeouts, your own) runs first. `.ShortCircuit()`, added in .NET 8, marks the endpoint so the routing middleware executes it on the spot, optionally setting a status code, and returns. Middleware before `UseRouting` still runs; with `WebApplication`'s implicit routing at the start of the pipeline, that is almost nothing. `MapShortCircuit(404, "robots.txt", "wp-admin")` maps `robots.txt/{**catchall}` and `wp-admin/{**catchall}` to a no-op delegate with `ShortCircuit(404)` and `Order = int.MaxValue`, so real endpoints still win. A short-circuited endpoint carrying authorization, CORS or antiforgery metadata throws an `InvalidOperationException`, because that middleware will never run; it also bypasses any authorization fallback policy. And if `robots.txt` exists in `wwwroot` served by `UseStaticFiles` after the implicit routing, the short circuit answers first with 404.
code
csharp · 15 linesvar builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
var app = builder.Build();
// Scanner probes get a bare 404 without running any middleware after routing
app.MapShortCircuit(404, "wp-admin", ".env", "phpmyadmin");
// Public, cheap liveness probe: skip auth, logging and the rest
app.MapGet("/health/live", () => TypedResults.Ok()).ShortCircuit();
// Would throw InvalidOperationException on every request:
// app.MapGet("/admin/ping", () => "pong").RequireAuthorization().ShortCircuit();
app.MapGet("/api/catalogue", () => TypedResults.Ok());
app.Run();go deeper
Know that MapShortCircuit returns a fixed status for listed paths without running the rest of the app's middleware.
Explain that the endpoint runs inside the routing middleware, what ShortCircuit's status code does, and MapShortCircuit's catch-all routes and order.
Use short-circuiting for probes and scanner noise while avoiding its traps: security metadata exceptions, fallback-policy bypass and shadowed static files.
Decide which traffic should be dropped in the app versus at the edge, keeping security controls from being bypassed by convenience.
## Normal endpoint execution In ASP.NET Core, `UseRouting` (added implicitly by `WebApplication` at the start of the pipeline when you do not call it) **selects** an endpoint and stores it on `HttpContext`. The endpoint itself runs at the end of the pipeline. Everything registered in between runs first, for every request: - authentication and authorization; - CORS, rate limiting, request timeouts, output caching; - your logging and custom middleware. For scanner traffic hitting `/wp-admin/install.php` on a hardware wholesaler's API, all that work is wasted. ## ShortCircuit on an endpoint `ShortCircuit(int? statusCode = null)` adds short-circuit metadata to any endpoint. When the routing middleware matches such an endpoint, it: 1. checks for authorization, CORS or form-method antiforgery metadata and throws `InvalidOperationException` if it finds any (unless `RouteOptions.SuppressCheckForUnhandledSecurityMetadata` is set); 2. sets the status code if one was given; 3. runs the endpoint's request delegate **right there**, and returns without calling the rest of the pipeline. ```csharp app.MapGet("/health/live", () => "ok").ShortCircuit(); ``` The liveness probe above skips authentication, logging middleware and everything else after routing. Middleware placed **before** `UseRouting` is unaffected, which only matters when you call `UseRouting` yourself. ## MapShortCircuit for prefixes `MapShortCircuit(int statusCode, params string[] routePrefixes)` is the bulk version: - for each prefix it maps `{prefix}/{**catchall}` (a catch-all is optional, so `/robots.txt` itself matches); - the delegate does nothing, so the response is just the status code; - each endpoint gets `ShortCircuit(statusCode)` and **`Order = int.MaxValue`**, so any real endpoint for the same path is preferred; - it returns an `IEndpointConventionBuilder`, so you can chain conventions such as `RequireHost`. ```csharp app.MapShortCircuit(404, "robots.txt", "favicon.ico", "wp-admin", ".env"); ``` The routing middleware logs an information message when it short-circuits, which helps confirm it is working. ## Traps | Trap | What happens | |---|---| | `.ShortCircuit()` on an endpoint with `RequireAuthorization()` or `[Authorize]` | `InvalidOperationException` per request: the authorization middleware would never run | | Same with `RequireCors` / `[EnableCors]`, or antiforgery validation on a form POST | `InvalidOperationException` | | An app-wide authorization **fallback policy** | not applied: the endpoint has no authorization metadata and `UseAuthorization` never runs | | `MapShortCircuit(404, "robots.txt")` while `wwwroot/robots.txt` is served by `UseStaticFiles` | with implicit routing at the start, the short-circuit endpoint matches first and the file is never served | | Request timeouts, rate limiting or HTTP logging configured for the route | silently skipped | The fallback-policy row deserves attention: a short-circuited endpoint is effectively anonymous, so use it only for responses that are safe for anyone. ## Seeing it work With the `Microsoft.AspNetCore.Routing` log category at `Information`, the routing middleware logs "The endpoint '...' is being executed without running additional middleware." for every short-circuited request, and endpoints created by `MapShortCircuit` have display names starting with `ShortCircuit`, so they are easy to spot. A quick check is to add a logging middleware after routing and confirm it stays silent for `/wp-admin/...` while it logs `/api/catalogue`. ## When to use which - **`MapShortCircuit`** for paths you never serve: bot probes, well-known files you do not have, legacy prefixes. Cheap 404s or 410s with no logging noise. - **`ShortCircuit()`** for real endpoints that are public, cheap and high-volume, such as liveness probes, where the pipeline adds only cost. - Neither for anything that needs authentication, CORS, antiforgery, rate limiting or auditing. ## What interviewers listen for - The endpoint executes inside the routing middleware, skipping everything after it. - `Order = int.MaxValue` on `MapShortCircuit`, and the optional status code on `ShortCircuit`. - The security metadata exception and the fallback-policy bypass.
- Why does MapShortCircuit give its endpoints Order = int.MaxValue?So that it never shadows a real endpoint. Routing prefers the lower order when several endpoints match, so if the app later maps a genuine `/robots.txt` endpoint or asset endpoint, that one is selected and the short circuit only catches what nothing else handles.
- Is a short-circuited endpoint covered by the app's authorization fallback policy?No. The authorization middleware runs after routing, and a short-circuited endpoint executes inside the routing middleware, so `UseAuthorization` never sees the request. The fallback policy is not endpoint metadata, so the security-metadata check does not throw either; the endpoint is simply public. Short-circuit only responses anyone may receive.
- ShortCircuit skips only the authorization middleware; everything else still runs.
- MapShortCircuit takes priority over any real endpoint mapped for the same path.
- Adding RequireAuthorization to a short-circuited endpoint makes it authenticate first.
- A fallback authorization policy still protects short-circuited endpoints.
- MapShortCircuit returns the status code only for the exact prefix, not deeper paths.
Assumes ASP.NET Core 10; ShortCircuit and MapShortCircuit arrived in .NET 8 and are unchanged in 10.