question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Route Short Circuit middleware

See original GitHub issue

Background and Motivation

Browsers and bots often probe servers for well known paths like favicon.ico. Normally the middleware pipeline runs to completion before rejecting a request as 404.

Services can reduce resource usage and log noise by filtering out known requests early in the pipeline. We can provide a middleware to do this efficiently.

Proposed API

Project/Assembly: Microsoft.AspNetCore.Routing

namespace Microsoft.Extensions.DependencyInjection;

+ public static class RouteShortCircuitIServiceCollectionExtensions
{
+     public static IServiceCollection AddRouteShortCircuit(this IServiceCollection services, Action<RouteShortCircuitOptions> configure) { }
}

namespace Microsoft.AspNetCore.Builder;

+ public static class RouteShortCircuitIApplicationBuilderExtensions
+ {
+     public static IApplicationBuilder UseRouteShortCircuit(this IApplicationBuilder builder) { }
+ }

namespace Microsoft.AspNetCore.Routing;

+ public class RouteShortCircuitOptions
+ {
+     // Exact path matches, empty by default
+     public ISet<string> Paths { get; } = new HashSet<string>(StringComparer.OridinalIgnoreCase);
+     // Any path that starts with these, empty by default
+     public ISet<string> PathsStartWith { get; } = new HashSet<string>(StringComparer.OridinalIgnoreCase);
+     public RequestDelegate OnRejected { get; set; }
+ }

Usage Examples

services.AddRouteShortCircuit(options =>
{
    options.Paths.Add("/favicon.ico");
    options.OnRejected(context => { /* metrics */ });
});
...
app.UseRouteShortCircuit();

Alternative:

Build this into UseRouting (https://github.com/dotnet/aspnetcore/issues/43642) and have it terminate immediately with a 404 if such an endpoint is matched.

We could also allow short circuiting to specify an endpoint/delegate to execute to produce a custom response.

Project/Assembly: Microsoft.AspNetCore.Routing

namespace Microsoft.Extensions.DependencyInjection;

+ public static class RouteShortCircuitEndpointConventionBuilderExtensions
{
+     public static IEndpointConventionBuilder ShortCircuit(this IEndpointConventionBuilder builder) { }
}

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Reactions:1
  • Comments:36 (31 by maintainers)

github_iconTop GitHub Comments

3reactions
tekiancommented, Jan 18, 2023

Oh I see what you mean. We’d advise to add routing as one of the first middlewares, claiming and assuming that the overhead to compute a route is negligent even for cases where we want to short circuit. And we’d sequence the rest of the middlewares after it, even those that are agnostic of the path.

1reaction
Kahbazicommented, Feb 1, 2023

@Tratcher Did you mean to write IEndpointRouteBuilder as argument for MapShortCircuit?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core Middleware
When a middleware short-circuits, it's called a terminal middleware because it prevents further middleware from processing the request. Migrate ...
Read more >
How to Properly Short-Circuit Middleware in Django?
My idea was to introduce a new middleware in between the Django ones and the custom ones of the project (commented as "Custom...
Read more >
Understanding Middleware In ASP.NET Core
But a middleware component can decide not to call the next piece of middleware in the pipeline. This is called short-circuiting or terminate...
Read more >
ASP.NET Core Middleware Components
This concept is called Short-Circuiting the Request Processing Pipeline. ... The UseRouting() middleware component is used to add Endpoint Routing ...
Read more >
Conditional Middleware based on request in ASP.NET Core
Assuming no short-circuiting (see above), if the request path start with '/api' then middleware Two will execute. Otherwise, middleware Three will execute.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found