我正在使用 Razor MVC,我想显示“~/Content/uploads”文件夹中的图像。
我想出了以下解决方案:
@foreach (FileInfo fileInfo in (new DirectoryInfo(Server.MapPath("~/Content/uploads"))
.GetFiles().Where(x => x.Extension == ".jpg"))) {
<img src="/@fileInfo
.FullName
.Substring(Server.MapPath("~/").Length)
.Replace("\\", "/")"
width="100">
}
代码的问题是我正在获取完整的文件路径
我正在删除 Server.MapPath() 前缀。
我怎样才能简化这段代码?
您可以使用网址助手 http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper%28v=vs.118%29.aspxRazor 页面上提供的类。
@foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/Content/uploads"), "*.jpg"))
{
var img = new FileInfo(imgPath);
<img src="@Url.Content(String.Format("~/Content/uploads/{0}", img.Name))" />
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)