C# Server.MapPath 从根目录返回两个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9725584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:35:24  来源:igfitidea点击:

Server.MapPath to go two folder back from root

c#asp.netiis

提问by petko_stankoski

Here is how I do it:

这是我如何做到的:

HttpContext.Current.Server.MapPath(@"~\~\~\Content\")

HI know that '.' is for the root of the project, but how to go a few folders back?

嗨知道'。是针对项目的根目录,但是如何返回几个文件夹呢?

采纳答案by Rup

If you really need the grandparent path, you can get it from the root path using Path.GetDirectoryName():

如果您确实需要祖父路径,则可以使用Path.GetDirectoryName()以下命令从根路径获取它:

string root = Server.MapPath("~");
string parent = Path.GetDirectoryName(root);
string grandParent = Path.GetDirectoryName(parent);

But your web app very likely won't have permission to read or write there - I'm not sure what you're going to do with it.

但是您的网络应用程序很可能没有权限在那里读取或写入 - 我不确定您将用它做什么。

回答by Adriano Repetti

Start with the root of your site with ~and specify the full path: ~/Archive/Content.

从您网站的根目录开始,~并指定完整路径:~/Archive/Content.

You can't go back above site rootbecause of security restrictions, see also this articlefrom other solutions.

由于安全限制,您无法回到站点根目录之上,另请参阅其他解决方案中的这篇文章

IIS purposefully prevents serving up content directly that is outside of the site path. However, you can create a virtual directory in IIS and have ISAPI Rewrite point to that. For example, create a virtual directory called /staticfiles that points to c:\test\data\static-files. As far as IIS is concerned, that's directly off of the root of the site in a folder called /staticfiles.

IIS 有目的地阻止直接提供站点路径之外的内容。但是,您可以在 IIS 中创建一个虚拟目录,并让 ISAPI 重写指向该目录。例如,创建一个名为 /staticfiles 的虚拟目录,指向 c:\test\data\static-files。就 IIS 而言,它直接位于名为 /staticfiles 的文件夹中的站点根目录之外。

回答by David Smit

You can use Parent.Parent.FullName

您可以使用Parent.Parent.FullName

  string grandParent  = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/")).Parent.Parent.FullName;

回答by eric1825

Since you are using MapPath you are getting returned a physical path(\) from a virtual path(/).

由于您使用的是 MapPath,因此您将从虚拟路径 (/) 中返回物理路径 (\)。

Creating a DirectoryInfo object or using a Path utility method starting from your child app root won't necessarily give you what you expect unless your virtual parent and virtual grandparent have the same hierarchy as your physical directory structure.

创建 DirectoryInfo 对象或使用从您的子应用程序根开始的 Path 实用程序方法不一定会为您提供您期望的结果,除非您的虚拟父级和虚拟祖父级与您的物理目录结构具有相同的层次结构。

My apps are not physically nested to match the Url depth. This could also be the case if a virtual directory is involved.

我的应用程序并未在物理上嵌套以匹配 Url 深度。如果涉及虚拟目录,也可能是这种情况。

Assuming a grandparent app is two virtual folders up this would get you the physical path...

假设祖父应用程序是两个虚拟文件夹,这将为您提供物理路径...

    string physicalGrandparentPath = HttpContext.Current.Server.MapPath("~/../../");

Using this would keep you safe from any virtual directory shuffle games going on in the IIS setup.

使用它可以保护您免受 IIS 设置中正在进行的任何虚拟目录随机播放游戏的影响。

I used this to see how far I could go up. I did not get an HttpException until I tried to go above wwwroot.

我用这个来看看我能走多远。在我尝试越过 wwwroot 之前,我没有收到 HttpException。