-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Endpoint information available before calling UseRouting() #50393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
For additional context, I maintain an internal framework that is built on top of ASP.NET; handles things such as logging, metrics, authentication and ensures all our services run middleware in the correct order. The previous version of this framework had to support .NET Framework, so it was cross-compiled for net472 (ASP.NET Core 2.2) and .net6. Due to this we still used the Startup class system. This is the first time we are running applications built on the minimal api infrastructure. |
One workaround I have tested for this is to run some custom middleware that checks if the file exists on disk, and if that is the case, sets the Endpoint information to null. The debug logging still shows the static file middleware is ignored, but it seems to return the file as expected. Is this a bad idea? Something I haven't though of? internal class FixEndpointMiddleware
{
private readonly RequestDelegate _next;
private readonly IWebHostEnvironment _hostEnvironment;
private readonly IOptions<StaticFileOptions> _options;
public FixEndpointMiddleware(
RequestDelegate next,
IWebHostEnvironment hostEnvironment,
IOptions<StaticFileOptions> options)
{
_next = next;
_hostEnvironment = hostEnvironment;
_options = options;
}
public async Task Invoke(HttpContext httpContext)
{
var path = httpContext.Request.Path;
// when using a combination of static file middleware, UsePathBase() and Razor wildcard convention
// the Endpoint information is set before the static file middleware runs.
// this middleware will exit when the Endpoint information has been set, even if it exists on disk.
// this is a workaround that always ensures it's null when the file exists on disk
if (path.StartsWithSegments(_options.Value.RequestPath, out var subpath))
{
var fileProvider = _options.Value.FileProvider ?? _hostEnvironment.WebRootFileProvider;
var file = fileProvider.GetFileInfo(subpath);
if (file.Exists)
{
var endpointFeature = httpContext.Features.Get<IEndpointFeature>();
if (endpointFeature != null)
{
endpointFeature.Endpoint = null;
}
}
}
await _next(httpContext).ConfigureAwait(false);
}
} |
We use Swashbuckle for SwaggerUI. This fails because it uses StaticFileMiddleware and EmbeddedFileProvider. When it requests the supporting js files, the content of my index razor page is returned. |
After some more digging, it looks like the problematic code is located in the UsePathBase() method. In #42876 this method was updated to include this code. if (app.Properties.TryGetValue(RerouteHelper.GlobalRouteBuilderKey, out var routeBuilder) && routeBuilder is not null)
{
return app.Use(next =>
{
var newNext = RerouteHelper.Reroute(app, routeBuilder, next);
return new UsePathBaseMiddleware(newNext, pathBase).Invoke;
});
} If we remove __GlobalEndpointRouteBuilder from Properties before calling UsePathBase(), then our code works as we except it to. Then we get static files, UsePathBase() and Razor wildcard to work at the same time. |
I'm having a similar problem with static files not being served properly when using UseStaticFiles is before UseRouting so if the file exist it should serve it even if there is a route for that path.
Output:
|
Is there an existing issue for this?
Describe the bug
We are updating applications to net7 and discovered problems serving static files. We have a Razor convention that maps any unknown url to /index. Using debug logging, we figured out that it didn't match any static file, and used the Razor convention instead.
Logging output:
I have created a small repro. This works as excepted on net6, but fails on net7 and net8.
If I remove UsePathBase(), things seem to be working again.
Expected Behavior
It should be possible to use
at the same time
Steps To Reproduce
https://github.com/kimbell/RazorStatics
Exceptions (if any)
No response
.NET Version
8.0.100-preview.7.23376.3
Anything else?
No response
The text was updated successfully, but these errors were encountered: