是否可以存储 wwwroot 文件夹以外的图像

Is it possible to store Images other than wwwroot folder

我将图像存储在文件夹名称 "Images" 中,该文件夹不在 wwwroot 文件夹中。 所以现在我无法访问它所以我的问题是 如何授予对包含图像的文件夹的访问权限,而该文件夹不是 wwwroot 的子文件夹?或者必须将这些文件放在 wwwroot 文件夹中?

是的Asp.Net Core 提供了一种方法来做到这一点。您可以从不在 wwwroot 文件夹中的图像目录提供图像。事实上,您可以从任何地方为它们提供服务,包括嵌入资源文件甚至数据库之外。

关键是在Startup.cs文件的Configure方法中注册一个FileProvider,这样Asp.Net核心就知道如何访问要服务的文件了。

因此,例如,在您的情况下,由于您希望从名为 Images 的目录中提供图像,因此假设您的目录层次结构如下所示:

wwwroot
     css
     images
     ...

 Images
     my-image.png

要从图像中提供 my-image.png,您可以使用以下代码:

 public void Configure(IApplicationBuilder app){
     app.UseStaticFiles(); // For the wwwroot folder

     app.UseStaticFiles(new StaticFileOptions(){
     FileProvider = new PhysicalFileProvider(
         Path.Combine(Directory.GetCurrentDirectory(), @"Images")),
         RequestPath = new PathString("/Images")
     });
 }

您可以在此处了解有关提供静态文件的更多信息:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files