我想做的是显示网络根文件夹中的图像,这就是我尝试执行此操作的方式:
下面的课程只是实验性的,只是我尝试的例子。目前正在读取的文件夹中只有一张图像。rootPath 也取自:_hostingEnvironment.WebRootPath
public class GetRandomImageForGalleryView : IGetRandomImageFromFolder
{
private string rootPath;
public GetRandomImageForGalleryView(string rootPath)
{
this.rootPath = rootPath;
}
public string[] getImage()
{
return ReadPhotosFromDirectory();
}
private string[] ReadPhotosFromDirectory()
{
string[] fileLocations = Directory.GetFiles(rootPath+"\\lib\\Images\\Nature");
return fileLocations;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我试图显示它的方式:
@model IGetRandomImageFromFolder
@{
ViewBag.Title = "Gallery";
}
<div class="container">
<div class="row">
@{
foreach (string item in Model.getImage())
{
<img src="@item" alt="Image" />
}
}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是没有输出,即使我将其更改为<img src="@Url.Content(item)" alt="Image" />
仍然没有发生。
如果我只是输出@item
它会显示图像的路径。也已 app.UseStaticFiles();
添加。
所以我的问题是我做错了什么,我错过了什么吗?以及如何正确地做到这一点?