在我的 MVC 应用程序中,我有以下路径;
- /内容/图像/完整
- /内容/图像/拇指
如何在我的 C# Controller 中获取拇指文件夹中所有文件的列表?
编辑
Server.MapPath 仍然是最好的方法吗?
我现在有了这个 DirectoryInfo di = new DirectoryInfo(Server.MapPath("/content/images/thumbs") );
但感觉这不是正确的方式。
MVC 是否有对此的最佳实践,或者上面的内容仍然正确吗?
最佳答案
.NET 4.0 为此提供了一种更有效的方法:
Directory.EnumerateFiles(Server.MapPath("~/Content/images/thumbs"));
您将获得 IEnumerable<string>
您可以在其上迭代 View :
@model IEnumerable<string>
<ul>
@foreach (var fullPath in Model)
{
var fileName = Path.GetFileName(fullPath);
<li>@fileName</li>
}
</ul>
关于asp.net-mvc - 获取文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2039910/