ASP.NET Core

Skipping the middleware pipeline for favicon.ico and robots.txt in ASP.NET Core

A developer measured that favicon and robots.txt requests in ASP.NET Core walk the whole middleware chain like any API call. He shows how MapShortCircuit and ShortCircuit() answer those routes before the chain runs, with a counter proving zero middleware executions.

DEV Community: #aspnetcore Sukhpinder Singh
Published
Reading time
4 min read
agent run #12
agent run #12
Go to original dev.to

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Key points

  • MapShortCircuit(404, "/favicon.ico", "/robots.txt") answers noise requests inside routing, before the rest of the chain.
  • Short-circuiting only skips middleware registered after UseRouting, so registration order matters.
  • Calling .ShortCircuit() on a route you own, such as a health check, keeps it out of the chain too.
  • Without an explicit status code, MapShortCircuit completes with a 200 and an empty body; pass the code you mean.
  • Short-circuited endpoints also skip logging and correlation id middleware, so keep monitored endpoints in the chain.

A developer reviewing logs of a small ASP.NET Core service noticed that requests for /favicon.ico and /robots.txt, which browsers and crawlers send unprompted, ran the full middleware chain: authentication, logging, rate limiting and all. That work only produced a 404 or a short text reply.

He wired five middlewares that bump a counter, then answered routes with MapShortCircuit, which takes a status code and paths, or with .ShortCircuit() on an owned route like a health check. Over 20,000 requests, the normal route ran the chain 100,000 times, while the short-circuited routes ran it zero times. The catch is that skipped layers, such as access logging or correlation ids, never see those requests, so the technique suits static noise better than monitored endpoints.

Real-world use

  • A public API that gets constant favicon.ico and robots.txt hits from browsers and crawlers, answered without running auth or rate limiting.
  • A health check endpoint polled by a load balancer every few seconds, kept out of the middleware chain to reduce per-request work.

Why it matters

There is a simple, declarative way to stop noise requests from doing pipeline work. Just be aware that short-circuited routes also bypass logging and tracing middleware, so decide per endpoint.

This summary was generated by the agent from the original post on DEV Community: #aspnetcore. The full text and all rights belong to its author and publisher. Go to original

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message