I have a folder under "wwwroot"
that I don't want to include when returning static files.
I want to return various directories under "wwwroot"
(for instance "wwwroot/images"
) and this middleware step enables that:
app.UseStaticFiles();
I can pass this StaticFileOptions
to configure what's returned.
I want to exclude a directory (for instance "wwwroot/node_modules"
) and not allow any statif files to be served from that.
There doesn't appear to be a filter I can use on StaticFileOptions
- how should I apply that filter?
Don't look for an option on the static files middleware - instead any middleware can be filtered using MapWhen
or UseWhen
. This allows you to only register the middleware for routes where a condition is passed.
For instance, to exclude "wwwroot/node_modules"
:
app.UseWhen(
context => !context.Request.Path.StartsWithSegments("/node_modules"),
appBuilder => appBuilder.UseStaticFiles());
This will now only apply the static files middleware when the route doesn't start with "/node_modules"
.
UseWhen
falls through to the next middleware if it failsMapWhen
only goes on to the next middleware if the context filter applies