在.NET7中,有没有办法只为特定的文件模式提供静态文件?
目前我有一个普通的老式:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(storageDirectory),
RequestPath = "/",
});
但我在寻找更多的链接
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(storageDirectory),
RequestPath = "/[a-b0-9\-]+/.+"
});
不知何故,这有可能吗?
[更新1]
我可以在源代码中看到,路径用StartsWithSegments
进行了判断:
https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/StaticFiles/src/Helpers.cs个
internal static bool TryMatchPath(HttpContext context, PathString matchUrl, bool forDirectory, out PathString subpath)
{
var path = context.Request.Path;
if (forDirectory && !PathEndsInSlash(path))
{
path += new PathString("/");
}
if (path.StartsWithSegments(matchUrl, out subpath))
{
return true;
}
return false;
}
看起来我需要编写一个定制的中间件?