Add Route Short Circuit middleware
See original GitHub issueBackground 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:
- Created 8 months ago
- Reactions:1
- Comments:36 (31 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@Tratcher Did you mean to write
IEndpointRouteBuilder
as argument forMapShortCircuit
?