[英]Serving and Verifying network share images in Asp.Net Core 3.1 Web Application (Razor Pages)
I have a.network file share which contains images that are served from various applications.我有一个 .network 文件共享,其中包含从各种应用程序提供的图像。 In an older application we simply created a Virtual Directory in IIS and used Server.MapPath(...) to get the correct path.在一个较旧的应用程序中,我们只是在 IIS 中创建了一个虚拟目录,并使用 Server.MapPath(...) 来获取正确的路径。 This no longer exists in Asp.Net Core 3.1.这在 Asp.Net Core 3.1 中不再存在。
So I added a /Data folder to my project and then added an additional app.UseStaticFiles
instance to map the /Data folder to the.network share path.所以我在我的项目中添加了一个/Data文件夹,然后将一个额外的app.UseStaticFiles
实例添加到 map /Data文件夹到 .network 共享路径。
Here is the code from the Configure(...)
function in Startup.cs
:这是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();
});
}
Issue问题
I need to be able to determine if a physical image exists on the.network share prior to displaying it.在显示之前,我需要能够确定 .network 共享上是否存在物理图像。
What Works什么有效
I can display an image using the tag within the Razor page markup just fine and confirmed it is from the.network share as follows:我可以使用 Razor 页面标记中的标签显示图像,并确认它来自 .network 共享,如下所示:
<img src="~/Data/PathInNetworkDrive/placeholder.jpg" alt="placholder test" class="card-img" />
What does NOT Work什么不起作用
I need to be able to determine if an image file physically exists on the.network share within the C# code behind in the main page.我需要能够确定图像文件是否实际存在于主页后面的 C# 代码内的 .network 共享中。
Everything I have tried gives me an error or just does not find a file when I know there are thousands of files out there.当我知道那里有成千上万的文件时,我尝试过的一切都会给我一个错误或只是找不到文件。
This is not a security issue at least I don't think it is since I am able to get to the images from the Razor page markup described above.这不是安全问题,至少我认为不是,因为我能够从上述 Razor 页面标记获取图像。
What I have tried我试过的
I have tried using the environment: var storagePhoto = _env.ContentRootPath combined with '/Data' folder with simple code:我试过使用环境: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
Tried adding a File Provider in Startup.cs and then injecting in razor page:尝试在 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}");
}
}
...
}
Any help would be welcome欢迎任何帮助
Once I corrected the path to the.network share that fixed the issue of finding out whether a photo existed or not - turns out I made a mistake by modifying the config on the server which worked but was wrong.一旦我更正了 .network 共享的路径,解决了查明照片是否存在的问题 - 事实证明我通过修改服务器上的配置犯了一个错误,但这是错误的。 It was \\server\folder
when it should have been \\\\server\\folder
- dumb mistake.当它应该是\\\\server\\folder
时它是\\server\folder
folder - 愚蠢的错误。
Second, I had the additional app.UseStaticFiles
function configured incorrectly, my fault for misreading an example (yep another mistake), this is how it should be:其次,我有额外的app.UseStaticFiles
function 配置不正确,我误读了一个例子(是的另一个错误),这应该是这样的:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider("\\\\server\\folder"),
RequestPath = "/Data"
});
The path in my initial Question was on entered as the RequestPath
and not passed into the PhysicalFileProvider(...)
function.我最初问题中的路径是作为RequestPath
输入的,没有传递到PhysicalFileProvider(...)
function。
Third, I had to remove the IIS Virtual Directory from the IIS app setup as it was blocking the new static file path.第三,我不得不从 IIS 应用程序设置中删除 IIS 虚拟目录,因为它阻止了新的 static 文件路径。
Security was correct already but we double checked it.安全性已经正确,但我们仔细检查了它。
So, turns out that the path I was on was correct but I made a couple of mistakes.所以,事实证明我走的路是正确的,但我犯了几个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.