Closed
Description
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 (#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) { }
}