Endpoint routing can not invoke a static file middleware
See original GitHub issueDescribe the bug
The static file middleware does not allow any form of serving of files when used in combination with endpoint routing. Maybe I am missing something here, but this prevents me from doing stuff like this:
Notice I am essentially trying to add auth to my Swagger UI without writing a custom middleware.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
var swaggerUiDelegate = endpoints.CreateApplicationBuilder().UseSwaggerUI(options =>
{
// Set documentation route to /docs
options.RoutePrefix = "docs";
})
.Build();
endpoints.Map("docs/{*wildcard}", swaggerUiDelegate).RequireAuthorization("Swagger");
});
To Reproduce
The above code should work if you essentially just add Swashbuckle to your project and attempt to add auth to your Swagger UI.
Exceptions (if any)
InvalidOperationException: The request reached the end of the pipeline without executing the endpoint: ‘docs/{*wildcard}’. Please register the EndpointMiddleware using ‘IApplicationBuilder.UseEndpoints(…)’ if using routing.
Further technical details
- ASP.NET Core version = 3.1
- Include the output of
dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 3.1.201
Commit: b1768b4ae7
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.15
OS Platform: Darwin
RID: osx.10.15-x64
Base Path: /usr/local/share/dotnet/sdk/3.1.201/
Host (useful for support):
Version: 5.0.0-preview.7.20364.11
Commit: 53976d38b1
.NET SDKs installed:
2.1.805 [/usr/local/share/dotnet/sdk]
3.1.201 [/usr/local/share/dotnet/sdk]
3.1.302 [/usr/local/share/dotnet/sdk]
5.0.100-preview.7.20366.6 [/usr/local/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.17 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.17 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0-preview.7.20365.19 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.17 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.20 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0-preview.7.20364.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
- The IDE (VS / VS Code/ VS4Mac) you’re running on, and it’s version Visual Studio 2019 for Mac
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:17 (10 by maintainers)
Top Results From Across the Web
Issue with serving some static files within ASP.NET Core ...
It seems that all files that reside in node_modules should be routed to my static files middleware, which is not happening. Thanks.
Read more >MVC1005: Cannot use UseMvc with Endpoint Routing
Using MVC via UseMvc or UseMvcWithDefaultRoute requires an explicit opt-in inside Startup.ConfigureServices . This is required because MVC ...
Read more >Converting a terminal middleware to endpoint routing in ...
In this post I provide an overview of the new endpoint routing system, and show how you can use it to create endpoints...
Read more >Static files in ASP.NET Core
Learn how to serve and secure static files and configure static file hosting middleware behaviors in an ASP.NET Core web app.
Read more >Handling SPA Fallback Paths in a Generic ASP.NET Core ...
Fixing Route Fallbacks. So, my LiveReloadServer application is a generic Web server which primarily allows you to serve static files.
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
It should work but I think you can simplify it. You don’t need a custom middleware, or to restore the endpoint value after the middleware.
A completely different way of doing this is setting the FallbackPolicy so that all requests require authentication. https://docs.microsoft.com/en-us/aspnet/core/security/authorization/secure-data?view=aspnetcore-3.1#require-authenticated-users
Routing endpoints are considered terminal, there’s no concept right now of an optional handler. Even if the request did re-enter the main pipeline, the endpoint executing middleware is itself terminal and placed at the end of the pipeline, there’d be nowhere to go but 404.