Skip to content

[Bug]: Under MapSwaggerUI incorrect paths will show InvalidOperationException instead of 404 #4007

@leotsarev

Description

@leotsarev

Describe the bug

Thanks for Swashbuckle — it's been a staple of ASP.NET API development for years.

Problem:

When using MapSwaggerUI, any request to a path under the swagger route that SwaggerUI doesn't recognize (e.g.
/swagger/ui/images/favicon-16x16-png from a crawler) throws.

Stack trace shows SwaggerUIMiddleware.Invoke at the top, called through auth middleware — which is expected for
endpoint-routed handlers.

Root cause:

MapSwaggerUI creates an endpoint sub-pipeline where SwaggerUIMiddleware is the handler. When the middleware encounters
a path it doesn't serve, it calls _next(context). In that sub-pipeline, _next leads to the end-of-pipeline guard,
which throws because no response was written.

UseSwaggerUI doesn't have this problem since it's not endpoint-routed — unknown paths fall through normally.

Expected behavior

MapSwaggerUI should return 404 for paths it doesn't recognize rather than letting the request reach
the end of the sub-pipeline unhandled.

Actual behavior

InvalidOperationException: The request reached the end of the pipeline without executing the endpoint:
'swagger/{**path}'. Please register the EndpointMiddleware using 'IApplicationBuilder.UseEndpoints(...)' if using
routing.

Steps to reproduce

No response

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

10.2.1

.NET Version

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions