From 48c2cb6fae5111fd8121acd3fef0916341fe74df Mon Sep 17 00:00:00 2001 From: martincostello Date: Tue, 16 Jun 2026 14:31:51 +0100 Subject: [PATCH] Fix InvalidOperationException if no route matches Fix `InvalidOperationException` being thrown if no match for the route pattern is found while executing the request pipelline. Resolves #3984. --- .../ReDocBuilderExtensions.cs | 18 ++++++++++++++++-- .../SwaggerBuilderExtensions.cs | 17 +++++++++++++++-- .../SwaggerUIBuilderExtensions.cs | 16 +++++++++++++++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs index a234117fc4..77b639782a 100644 --- a/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Swashbuckle.AspNetCore.ReDoc; @@ -52,7 +53,20 @@ public static IEndpointConventionBuilder MapReDoc( .UseReDoc(options) .Build(); - return endpoints.Map(GetRoutePattern(options.RoutePrefix), pipeline); + return endpoints.Map(GetRoutePattern(options.RoutePrefix), async (context) => + { + var endpoint = context.GetEndpoint(); + context.SetEndpoint(null); + + try + { + await pipeline(context); + } + finally + { + context.SetEndpoint(endpoint); + } + }); } private static ReDocOptions ResolveOptions(IServiceProvider serviceProvider, Action setupAction) diff --git a/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs index 0da63214ee..f5bb59f77a 100644 --- a/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs @@ -1,7 +1,7 @@ +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Patterns; using Microsoft.AspNetCore.Routing.Template; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Swashbuckle.AspNetCore.Swagger; @@ -47,7 +47,20 @@ public static IEndpointConventionBuilder MapSwagger( .UseSwagger(Configure) .Build(); - return endpoints.MapMethods(pattern, [HttpMethods.Get, HttpMethods.Head], pipeline); + return endpoints.MapMethods(pattern, [HttpMethods.Get, HttpMethods.Head], async (context) => + { + var endpoint = context.GetEndpoint(); + context.SetEndpoint(null); + + try + { + await pipeline(context); + } + finally + { + context.SetEndpoint(endpoint); + } + }); void Configure(SwaggerOptions options) { diff --git a/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs index c5b71615ba..b15c44ed75 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -60,7 +61,20 @@ public static IEndpointConventionBuilder MapSwaggerUI( .UseSwaggerUI(options) .Build(); - return endpoints.Map(GetRoutePattern(options.RoutePrefix), pipeline); + return endpoints.Map(GetRoutePattern(options.RoutePrefix), async (context) => + { + var endpoint = context.GetEndpoint(); + context.SetEndpoint(null); + + try + { + await pipeline(context); + } + finally + { + context.SetEndpoint(endpoint); + } + }); } private static SwaggerUIOptions ResolveOptions(IServiceProvider serviceProvider, Action setupAction)