在Asp.Net Core 3.1 Web应用程序中提供和验证网络共享映像剃刀页 答案

问题描述

我有一个网络文件共享,其中包含从各种应用程序提供的图像。在较旧的应用程序中,我们只是在IIS中创建了一个虚拟目录,并使用Server.MapPath(...)来获取正确的路径。这在Asp.Net Core 3.1中不再存在。

因此,我在项目中添加了 / Data 文件夹,然后添加了另外一个app.UseStaticFiles实例以将 / Data 文件夹映射到网络共享路径。

这是Configure(...)Startup.cs函数的代码:

        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();
            });
        }

问题

在显示共享之前,我需要能够确定网络共享上是否存在物理图像。

有效的方法

我可以使用Razor页面标记中的标签显示图像,并确认它来自网络共享,如下所示:

    <img src="~/Data/PathInNetworkDrive/placeholder.jpg" alt="placholder test" class="card-img" />

什么不起作用

我需要能够在主页后面的C#代码中确定网络共享上是否物理存在图像文件。

当我知道有成千上万个文件时,我尝试过的所有操作都会给我一个错误,或者就是找不到文件。

这不是安全问题,至少我不认为这是安全问题,因为我能够从上述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中添加文件提供程序,然后注入到剃须刀页面:

    // 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}");
            }        
        }
        ...
    }

欢迎任何帮助

解决方法

答案

一旦我更正了网络共享的路径,从而解决了查找照片是否存在的问题-事实证明,我通过修改服务器上有效但错误的配置来弄错了。应该是\\server\folder,这是\\\\server\\folder-愚蠢的错误。

第二,我没有正确配置附加的app.UseStaticFiles函数,我的错误是读了一个示例(是另一个错误),这应该是这样的:

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider("\\\\server\\folder"),RequestPath = "/Data"
            });

我最初的问题中的路径被输入为RequestPath,而没有传递到PhysicalFileProvider(...)函数中。

第三,我不得不从IIS应用程序安装程序中删除IIS虚拟目录,因为它阻止了新的静态文件路径。

安全性已经正确,但我们再次对其进行了检查。

因此,事实证明我所走的道路是正确的,但我犯了一些错误。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...