![](/img/trans.png)
[英]Set default page in ASP.NET Core 3.1 MVC+Razor Pages+Web API
[英]Serving and Verifying network share images in Asp.Net Core 3.1 Web Application (Razor Pages)
我有一个 .network 文件共享,其中包含从各种应用程序提供的图像。 在一个较旧的应用程序中,我们只是在 IIS 中创建了一个虚拟目录,并使用 Server.MapPath(...) 来获取正确的路径。 这在 Asp.Net Core 3.1 中不再存在。
所以我在我的项目中添加了一个/Data文件夹,然后将一个额外的app.UseStaticFiles
实例添加到 map /Data文件夹到 .network 共享路径。
这是Startup.cs
中Configure(...)
function 的代码:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
. . .
// serves static js/css/whatever files from /wwwroot
app.UseStaticFiles();
// serves static image files from /Data
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Data")),
RequestPath = "//networkdriveshare/pathToImages"
});
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
问题
在显示之前,我需要能够确定 .network 共享上是否存在物理图像。
什么有效
我可以使用 Razor 页面标记中的标签显示图像,并确认它来自 .network 共享,如下所示:
<img src="~/Data/PathInNetworkDrive/placeholder.jpg" alt="placholder test" class="card-img" />
什么不起作用
我需要能够确定图像文件是否实际存在于主页后面的 C# 代码内的 .network 共享中。
当我知道那里有成千上万的文件时,我尝试过的一切都会给我一个错误或只是找不到文件。
这不是安全问题,至少我认为不是,因为我能够从上述 Razor 页面标记获取图像。
我试过的
我试过使用环境:var storagePhoto = _env.ContentRootPath 结合 '/Data' 文件夹和简单的代码:
// finds nothing of course, ever
var storagePhoto = Path.Combine(_env.ContentRootPath, "Data");
if (System.IO.File.Exists(storagePhoto))
// found
else
// not found
尝试在 Startup.cs 中添加文件提供程序,然后在 razor 页面中注入:
// Startup.cs
var imagesProvider = new PhysicalFileProvider(Path.Combine(_env.ContentRootPath, "Data"));
var compositeProvider = new CompositeFileProvider(imagesProvider);
services.AddSingleton<IFileProvider>(compositeProvider);
// Index.cshtml.cs
public class IndexModel : PageModel
{
private readonly IFileProvider _fileProvider;
private readonly ILogger<IndexModel> _logger;
private IWebHostEnvironment _env;
...
public IndexModel(ILogger<IndexModel> logger,
IFileProvider fileProvider,
IWebHostEnvironment env)
{
_logger = logger;
_env = env;
_fileProvider = fileProvider;
}
// blows up on 'GetDirectoryContents' just displays the path to /Data in the error message
public IActionResult OnGet()
{
try
{
var contents = _fileProvider.GetDirectoryContents(string.Empty);
//var filePath = Path.Combine("wwwroot", "js", "site.js");
//var fileInfo = provider.GetFileInfo(filePath);
}
catch (Exception ext)
{
Console.WriteLine($"{ext.Message}");
}
}
...
}
欢迎任何帮助
一旦我更正了 .network 共享的路径,解决了查明照片是否存在的问题 - 事实证明我通过修改服务器上的配置犯了一个错误,但这是错误的。 当它应该是\\\\server\\folder
时它是\\server\folder
folder - 愚蠢的错误。
其次,我有额外的app.UseStaticFiles
function 配置不正确,我误读了一个例子(是的另一个错误),这应该是这样的:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider("\\\\server\\folder"),
RequestPath = "/Data"
});
我最初问题中的路径是作为RequestPath
输入的,没有传递到PhysicalFileProvider(...)
function。
第三,我不得不从 IIS 应用程序设置中删除 IIS 虚拟目录,因为它阻止了新的 static 文件路径。
安全性已经正确,但我们仔细检查了它。
所以,事实证明我走的路是正确的,但我犯了几个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.