【问题标题】:Iterate through a site relative path to find files (ASP.NET MVC 3)遍历站点相对路径以查找文件 (ASP.NET MVC 3)
【发布时间】:2012-10-04 07:04:22
【问题描述】:

我的 Web 应用程序 (ASP.NET MVC 3) 中有一个文件夹,其中包含一组图像。我想使用 foreach 循环遍历文件夹以获取站点相对路径,然后我可以将其附加到图像标签。

例子

<div class="slides">
@foreach(string file in ?????)
{
    <img src="@file" alt="filename without extension">
}
</div>

我该怎么做?

注意:我当前的 foreach 循环正在尝试查看物理路径并返回此错误

Could not find a part of the path 'C:\Content\Images\Photography\Slides\'.

【问题讨论】:

  • 这种活动应该在控制器甚至模型级别完成,不是吗?即将一个可枚举的 uri 传递给视图
  • @ChristopherBales,我明白你的意思,但无论如何,这并不能解决问题。
  • 检查文件夹是否存在以及当前进程是否有读取权限。让它发挥作用后,请遵循 @ChristopherBales 给你的建议。
  • @w0lf,网络文件夹没有对应的 Directory.GeFiles 吗?
  • 你试过 HttpContext.Current.Server.MapPath 了吗?

标签: c# .net asp.net-mvc-3 web-applications


【解决方案1】:

假设您想枚举相对于网站根目录的路径中的文件,您可以这样做:

@foreach (var file in 
    Directory.GetFiles(Server.MapPath("~/Content/Images/Photography/Slides")))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多