I needed a custom file provider, and to access a request's HttpContext, I found a solution using IHttpContextAccessor:
app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments("/res", StringComparison.OrdinalIgnoreCase), appBuilder =>
{
appBuilder.UseStaticFiles(new StaticFileOptions
{
FileProvider = new HostResFileProvider(appBuilder.ApplicationServices.GetRequiredService<IHttpContextAccessor>(), builder.Environment),
RequestPath = "/res"
});
});
While that worked just fine, looking at that code snippet I just started to wonder, instead of setting up IHttpContextAccessor wouldn't it be possible to pass the ctx variable instead, something like this?
app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments("/res", StringComparison.OrdinalIgnoreCase), appBuilder =>
{
appBuilder.UseStaticFiles(new StaticFileOptions
{
FileProvider = new HostResFileProvider(ctx, builder.Environment),
RequestPath = "/res"
});
});
Now, this won't work, though is there a way making it work?