我正在使用MVC4 c#。我正在尝试通过读取图像文件夹的内容并执行foreach循环来加载我需要在页面上动态显示的图像。我不知道如何读取位于项目中的名为ImageFiles的文件夹的内容,而不是服务器的c:\。这是我使用的,它可以在我的本地计算机上工作但是当我使用../../filename/filename/ImageFiles
作为路径它不起作用。有人可以帮忙吗?
string filePath = @"../../Content/EventFiles/ImageFiles";
DirectoryInfo directory = new DirectoryInfo(filePath);
@foreach (FileInfo file in directory.GetFiles())
{
<\a href="../../Content/EventFiles/ImageFiles/@file.Name">
<\img src="/Content/EventFiles/ImageFiles/@file.Name" />
<\/a>
}
答案 0 :(得分:1)
您没有正确引用filePath。
在您的视图中尝试这个..
@{DirectoryInfo dir = new DirectoryInfo(Server.MapPath(Url.Content("~/Content/EventFiles/ImageFiles")));}
@foreach (var file in dir.GetFiles())
{
<img src="@Url.Content("~/Content/EventFiles/ImageFiles/" + file.Name)" />
}
答案 1 :(得分:0)
答案是Server.MapPath()。对于桌面应用程序,DirectoryInfo()可以工作,但对于Web应用程序,我必须使用Server.MapPath()。
由于