[英]Asp.Net Core StaticFiles include / exclude rules
我已經設置了 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"
});
我一直在搜索文檔並瀏覽 object 模型,但我不知道如何設置包含和排除過濾器。 我當前的代碼是過濾以.
(隱藏?但我在 Windows 上運行)我想顯示和下載這些文件,但隱藏其他類型,例如 *.json 和 web.config。
這有點像黑客,但它對我有用。 您可以創建一個在底層使用 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.