如何使用服务器。在ASP.NET中获取网站文件夹之外的位置

时间:2022-01-31 13:26:22

When my ASP.NET site uses documents (e.g. XML), I normally load the document as follows:

当我的ASP。NET site使用的是文档(如XML),我通常是这样加载的:

Server.MapPath("~\Documents\MyDocument.xml")

However, I would like to move the Documents folder out of the website folder so that it is now a sibling of the website folder. This will make maintaining the documents considerably easier.

但是,我想把文档文件夹移出网站文件夹,以便它现在是网站文件夹的兄弟姐妹。这将使文档的维护更加容易。

However, rewriting the document load code as follows:

但是,重写文档加载代码如下:

Server.MapPath("../../Documents/MyDocument.xml")

results in a complaint from ASP.NET that it cannot 'exit above the top directory'.

结果ASP投诉。它不能“退出上面的目录”。

So can anyone suggest how I can relatively specify the location of a folder outside the website folder? I really don't want to specify absolute paths for the obvious deployment reasons.

那么,有人能建议我如何相对地指定网站文件夹之外的文件夹的位置吗?出于明显的部署原因,我真的不想指定绝对路径。

Thanks

谢谢

David

大卫

4 个解决方案

#1


25  

If you know where it is relative to your web root, you can use Server.MapPath to get the physical location of your web root, and then Path class's method to get your document path.

如果您知道它相对于web根的位置,您可以使用服务器。获取web根的物理位置,然后获取路径类的方法来获取文档路径。

In rough unchecked code something like:

在未经检查的粗略代码中,如下内容:

webRootPath = Server.MapPath("~")
docPath = Path.GetFullPath(Path.Combine(rootPath, "../Documents/MyDocument.xml"))

Sorry if I got the Syntax wrong, but the Path class should be what you are after to play with real FS paths rather than the web type paths.

抱歉,如果语法错误,但是Path类应该是您想要使用真正的FS路径,而不是web类型路径。

The reason your method failed is that Server.MapPath takes a location on your web server and the one you gave is not valid, since it is "above" the top of the root of the server hierarchy.

您的方法失败的原因是服务器。MapPath在您的web服务器上获取一个位置,而您所提供的位置是无效的,因为它位于服务器层次结构的根目录的上方。

#2


5  

docPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\Documents\MyDocument.xml");

AppDomain.BaseDirectory returns current web application assembly directory path.

应用程序域中。BaseDirectory返回当前web应用程序组装目录路径。

#3


1  

If you need to resolve the path in either case absolute or relative (even outside the web app root folder) use this:

如果您需要解决任何情况下的绝对或相对的路径(甚至在web应用程序根文件夹之外),请使用以下方法:

public static class WebExtesions
{
    public static string ResolveServerPath(this HttpContextBase context, string path) {
        bool isAbsolute = System.IO.Path.IsPathRooted(path);
        string root = context.Server.MapPath("~");
        string absolutePath = isAbsolute ? 
                                    path : 
                                    Path.GetFullPath(Path.Combine(root, path));
        return absolutePath;
    }
}

#4


0  

If you want to specify the Location somewhere in harddrive , then its is not easily available on web environment. If files are smaller in size and quantity then you can keep it inside directory and point then using ~/path till directory.

如果您想指定harddrive中的某个位置,那么它在web环境中是不容易获得的。如果文件的大小和数量都比较小,那么您可以将它保存在目录中,然后使用~/path till目录。

But in some cases we used to do Request object. For more visit this link

但在某些情况下,我们使用Request object。更多信息请访问此链接

http://msdn.microsoft.com/en-us/library/5d5940ad.aspx

http://msdn.microsoft.com/en-us/library/5d5940ad.aspx

#1


25  

If you know where it is relative to your web root, you can use Server.MapPath to get the physical location of your web root, and then Path class's method to get your document path.

如果您知道它相对于web根的位置,您可以使用服务器。获取web根的物理位置,然后获取路径类的方法来获取文档路径。

In rough unchecked code something like:

在未经检查的粗略代码中,如下内容:

webRootPath = Server.MapPath("~")
docPath = Path.GetFullPath(Path.Combine(rootPath, "../Documents/MyDocument.xml"))

Sorry if I got the Syntax wrong, but the Path class should be what you are after to play with real FS paths rather than the web type paths.

抱歉,如果语法错误,但是Path类应该是您想要使用真正的FS路径,而不是web类型路径。

The reason your method failed is that Server.MapPath takes a location on your web server and the one you gave is not valid, since it is "above" the top of the root of the server hierarchy.

您的方法失败的原因是服务器。MapPath在您的web服务器上获取一个位置,而您所提供的位置是无效的,因为它位于服务器层次结构的根目录的上方。

#2


5  

docPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\Documents\MyDocument.xml");

AppDomain.BaseDirectory returns current web application assembly directory path.

应用程序域中。BaseDirectory返回当前web应用程序组装目录路径。

#3


1  

If you need to resolve the path in either case absolute or relative (even outside the web app root folder) use this:

如果您需要解决任何情况下的绝对或相对的路径(甚至在web应用程序根文件夹之外),请使用以下方法:

public static class WebExtesions
{
    public static string ResolveServerPath(this HttpContextBase context, string path) {
        bool isAbsolute = System.IO.Path.IsPathRooted(path);
        string root = context.Server.MapPath("~");
        string absolutePath = isAbsolute ? 
                                    path : 
                                    Path.GetFullPath(Path.Combine(root, path));
        return absolutePath;
    }
}

#4


0  

If you want to specify the Location somewhere in harddrive , then its is not easily available on web environment. If files are smaller in size and quantity then you can keep it inside directory and point then using ~/path till directory.

如果您想指定harddrive中的某个位置,那么它在web环境中是不容易获得的。如果文件的大小和数量都比较小,那么您可以将它保存在目录中,然后使用~/path till目录。

But in some cases we used to do Request object. For more visit this link

但在某些情况下,我们使用Request object。更多信息请访问此链接

http://msdn.microsoft.com/en-us/library/5d5940ad.aspx

http://msdn.microsoft.com/en-us/library/5d5940ad.aspx