ser*_*0ne 3 .net c# web-applications asp.net-mvc-3
我的Web应用程序(ASP.NET MVC 3)中有一个文件夹,其中包含一组图像.我想使用foreach循环遍历文件夹以获取站点相对路径,然后我可以将其附加到图像标记.
例
<div class="slides">
@foreach(string file in ?????)
{
<img src="@file" alt="filename without extension">
}
</div>
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
注意:我当前的foreach循环正在尝试查看物理路径并返回此错误
Could not find a part of the path 'C:\Content\Images\Photography\Slides\'.
Run Code Online (Sandbox Code Playgroud)
假设您想要枚举相对于网站根目录的路径中的文件,您可以这样做:
@foreach (var file in
Directory.GetFiles(Server.MapPath("~/Content/Images/Photography/Slides")))
Run Code Online (Sandbox Code Playgroud)