如何使用要使用Server.Mappath网站文件夹之外获得ASP.NET位置 [英] How to use Server.MapPath to get location outside website folder in ASP.NET

查看:167
本文介绍了如何使用要使用Server.Mappath网站文件夹之外获得ASP.NET位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET网站使用的文件(如XML),我通常加载文档如下:

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

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.

不过,改写文档加载code如下:

However, rewriting the document load code as follows:

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

在从ASP.NET投诉的结果,它不能'口上方的顶级目录。

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

所以,任何人都可以建议我怎么能比较指定的网站文件夹之外的文件夹的位置?我真的不想为明显的原因部署指定绝对路径。

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.

感谢

大卫

推荐答案

如果你知道它是相对于你的Web根目录,您可以使用使用Server.Mappath让你的Web根目录的路径上的phsical位置,然后方法(HTTP ://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx),让你的doument路径

If you know where it is relative to your web root you can use Server.Mappath to get the phsical location of your web root and then method on path (http://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx) to get your doument path.

在粗糙选中code是这样的:

In rough unchecked code something like:

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

很抱歉,如果我得到的语法错了,但Path类应该是你所追求的与真正的FS路径,而不是网络型路径玩了。

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.

您的方法失败的原因是使用Server.Mappath Web服务器ADN你给了一个在需要的位置是不是有效的,因为它是上面的顶部根服务器的层次。

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

这篇关于如何使用要使用Server.Mappath网站文件夹之外获得ASP.NET位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆