[英]Asp.Net Core StaticFiles include / exclude rules
I've setup static files and directory browsing like this:我已经设置了 static 文件和目录浏览,如下所示:
PhysicalFileProvider physicalFileProvider = new PhysicalFileProvider(somePath, ExclusionFilters.None);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = physicalFileProvider,
RequestPath = "/files",
ServeUnknownFileTypes = true
});
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = new PhysicalFileProvider(somePath),
RequestPath = "/files"
});
I've been searching documentation and browsing through object models but I can't figure out how to set the include and exclude filters.我一直在搜索文档并浏览 object 模型,但我不知道如何设置包含和排除过滤器。 My current code is filtering files starting with
.
我当前的代码是过滤以
.
(hidden? but I'm running on Windows ) I want to show and download these files but hide other types such as *.json and web.config. (隐藏?但我在 Windows 上运行)我想显示和下载这些文件,但隐藏其他类型,例如 *.json 和 web.config。
This is a bit of a hack, but it worked for me.这有点像黑客,但它对我有用。 You can make a new fileprovider that uses PhysicalFileProvider (or anything else really) under the hood, but hides files according to a pattern.
您可以创建一个在底层使用 PhysicalFileProvider(或其他任何东西)的新文件提供程序,但根据模式隐藏文件。
public class TplFileProvider : IFileProvider
{
private readonly IFileProvider fileProvider;
public TplFileProvider(string root)
{
fileProvider = new PhysicalFileProvider(root);
}
public TplFileProvider(string root, ExclusionFilters exclusionFilter)
{
fileProvider = new PhysicalFileProvider(root, exclusionFilter);
}
public IDirectoryContents GetDirectoryContents(string subpath)
{
return (IDirectoryContents) fileProvider.GetDirectoryContents(subpath).Where(i => isAllowed(i.Name));
}
public IFileInfo GetFileInfo(string subpath)
{
var file = fileProvider.GetFileInfo(subpath);
if (isAllowed(subpath))
{
return file;
}
else
{
return new NotFoundFileInfo(file.Name);
}
}
private static bool isAllowed(string subpath)
{
return subpath.EndsWith(".json") || subpath.Equals("web.config");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.