首页 > 解决方案 > 在 ASP.NET Core 3.1 中使用虚拟目录

问题描述

在 ASP.NET 框架 4.8 及以下版本中,Web 应用程序使用 IIS 来处理和显示数据。当我们需要使用文件夹和网络资源等其他资源时,我们可以轻松地在 IIS 中创建虚拟目录并映射到物理路径和我的应用程序工作主题,如普通文件夹。但在 Net Core 中,我们使用 Kestrel 和 IIS 作为代理和 Kestrel 无法识别的虚拟目录。我使用此代码但无法正常工作。将此部分写在配置函数中

app.UseFileServer(new FileServerOptions
{
 FileProvider = new PhysicalFileProvider(@"E:\\MyFiles"),
 RequestPath = new PathString("/PFiles"),
 EnableDirectoryBrowsing = true
});

这是我的代码:

var folder = Path.Combine(environment.WebRootPath, "temp");


                var fileName = Path.Combine(environment.WebRootPath, "temp\\test.jpeg");

                var basePath = Path.Combine(environment.WebRootPath, "PFiles");
               
                
                var yearPath = Path.Combine(basePath, "2020");
                var monthpath = Path.Combine(basePath, "2020", "06");

                if (!Directory.Exists(yearPath))
                {
                    Directory.CreateDirectory(yearPath);
                }
                if (!Directory.Exists(monthpath))
                {
                    Directory.CreateDirectory(monthpath);
                }

                var dpath = Path.Combine(basePath, "2020", "06");

                var destinationPath = Path.Combine(environment.WebRootPath, dpath);


                System.IO.File.Move(fileName, destinationPath);

但我收到拒绝访问错误或在文件夹内创建目录wwwroot。那么,我必须做什么?

标签: asp.net-coreiiskestrel

解决方案


您收到此错误是因为 iis 默认帐户没有足够的权限来访问该文件夹。

您可以尝试将 iis_iusrs 、 iusr 或 IIS AppPool<myappoolname> 权限分配给要在其中创建目录的文件夹。


推荐阅读