Server.MapPath() 未获取所需的路径
我有一个 asp.net 网站,具有以下目录:
C:\Users\Desktop\Testing\src\website
我有另一个名为“文件”的文件夹,位于此处:
C:\Users\Desktop\Testing\src\files
从我的项目内部,我尝试从“读取文件” files”文件夹中,我这样做是这样的:
var path = HttpContext.Current.Server.MapPath("/files");
我也尝试过:
var path = HttpContext.Current.Server.MapPath("..");
但它说无法映射路径'/files'。
这可能是什么原因?它能对我的 IIS 做点什么吗?我怎样才能让它工作?
谢谢你!
I have a asp.net website that has the following directory:
C:\Users\Desktop\Testing\src\website
I have another folder called "files" that is here:
C:\Users\Desktop\Testing\src\files
from inside my project i am trying to read files from the "files" folder, i am doing it like this:
var path = HttpContext.Current.Server.MapPath("/files");
I also tried :
var path = HttpContext.Current.Server.MapPath("..");
But it says Failed to map the path '/files'.
What could be the reason for this? could it do something with my IIS? How can i get this working??
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能这样做。
Server.MapPath
方法仅适用于相对于 Web 应用程序根目录的文件夹,在您的情况下为C:\Users\Desktop\Testing\src\website
。您无法使用此方法在层次结构中向上一级,因为您将离开此 ASP.NET 应用程序的控制域。要实现此目的,您必须使用绝对路径。例如,如果您想读取位于应用程序外部的某个文件:You can't do this. The
Server.MapPath
method only works with folders relative to the root of the web application which in your case isC:\Users\Desktop\Testing\src\website
. You cannot go one level up in the hierarchy using this method as you are leaving the domain of control of this ASP.NET application. To achieve this you will have to use an absolute path. For example if you want to read some file which is situated outside of your application:假设网站的虚拟目录映射到 .../src/website 您需要像这样获取“files”文件夹:
assuming the websites's virtual directory is mapped to .../src/website you need to get "files" folder like this: