遍历一个站点相对路径来查找文件(ASP.NET MVC 3) [英] Iterate through a site relative path to find files (ASP.NET MVC 3)

查看:287
本文介绍了遍历一个站点相对路径来查找文件(ASP.NET MVC 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹在我的Web应用程序(ASP.NET MVC 3),其中包含了一组图片。我想用一个foreach循环遍历文件夹中得到,我可以再追加到一个图像标记的部位相对路径。

示例

 < D​​IV CLASS =幻灯片>
@foreach(字符串文件?????)
{
    < IMG SRC =@文件ALT =文件名不带扩展名>
}
< / DIV>
 

我如何做到这一点?

注:我目前的foreach循环试图寻找通过物理路径,并返回该错误

 找不到路径C:\内容\图片\摄影\幻灯片\'的一部分。
 

解决方案

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

  @foreach(var文件
    Directory.GetFiles(使用Server.Mappath(〜/内容/图片/摄影/幻灯片)))
 

I have a folder in my web application (ASP.NET MVC 3) which contains a set of images. I want to use a foreach loop to iterate through the folder to get the site relative paths which I can then append to an image tag.

Example

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

How do I do this?

NOTE: my current foreach loop is trying to look through a physical path and returns this error

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

解决方案

Assuming you'd like to enumerate the files in a path relative to the website's root, you could do:

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

这篇关于遍历一个站点相对路径来查找文件(ASP.NET MVC 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆