Server.MapPath() 未获取所需的路径

发布于 2024-12-05 05:11:59 字数 491 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡忘如思 2024-12-12 05:11:59

你不能这样做。 Server.MapPath 方法仅适用于相对于 Web 应用程序根目录的文件夹,在您的情况下为 C:\Users\Desktop\Testing\src\website。您无法使用此方法在层次结构中向上一级,因为您将离开此 ASP.NET 应用程序的控制域。要实现此目的,您必须使用绝对路径。例如,如果您想读取位于应用程序外部的某个文件:

var data = File.ReadAllText(@"C:\Users\Desktop\Testing\src\files\somefile.txt");

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 is C:\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:

var data = File.ReadAllText(@"C:\Users\Desktop\Testing\src\files\somefile.txt");
夜唯美灬不弃 2024-12-12 05:11:59

假设网站的虚拟目录映射到 .../src/website 您需要像这样获取“files”文件夹:

var filesDir = Path.Combine(HttpContext.Current.Server.MapPath("~"), "../files/");

assuming the websites's virtual directory is mapped to .../src/website you need to get "files" folder like this:

var filesDir = Path.Combine(HttpContext.Current.Server.MapPath("~"), "../files/");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文