为什么波浪号和斜杠在Asp.net MVC应用程序中不起作用? [英] Why tilde and slash doesnt work in Asp.net MVC application?
本文介绍了为什么波浪号和斜杠在Asp.net MVC应用程序中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我编写此代码时
string[] filePaths = Directory.GetFiles(@"~/Content/Images/");
我得到 System.IO.DirectoryNotFoundException:找不到路径'C:\ Program Files(x86)\ IIS Express \ Content \ Images'的一部分
.我应该如何正确书写?我有图像"和内容"文件夹,并且我不想对路径进行硬编码.另外,当我使用html代码(例如< img src =〜/Content/Images/Img.jpg")
I get System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\IIS Express\Content\Images'
.
How should I write correctly? I have the folder "Images" and "Content", and I dont want to hardcode a path. Also, it works when I write in html code like <img src="~/Content/Images/Img.jpg" for example
推荐答案
将 HostingEnvironment.ApplicationPhysicalPath
与 Path.Combine
结合使用,例如:
string[] filePaths = Directory.GetFiles(Path.Combine
(HostingEnvironment.ApplicationPhysicalPath,
"Content/Images"));
或
使用 HttpContext.Current.Server.MapPath
,例如:
string[] filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath
(@"~/Content/Images/"));
这篇关于为什么波浪号和斜杠在Asp.net MVC应用程序中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文