C# 来自内部文件夹的 ASP.NET Server.Mappath 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1158530/
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-06 09:36:56  来源:igfitidea点击:

ASP.NET Server.Mappath problem from inner folders

c#asp.net

提问by Shyju

I have an ASP.NET application where in my APP_Code folder i have a class.In that i have the following code to read the content of an XML file which is in my root folder

我有一个 ASP.NET 应用程序,其中在我的 APP_Code 文件夹中有一个类。我有以下代码来读取位于我的根文件夹中的 XML 文件的内容

XmlDocument xmlSiteConfig = new XmlDocument();
xmlSiteConfig.Load(System.Web.HttpContext.Current.Server.MapPath("../myConfig.xml"));

My Root folder is having several folders with nested inner folders for some.From the first level of folders when i call the piece of code in the Appcode class,I am able to load the XML file correctly since the path is correct.Now if i call the same piece of code from an innner folder,I am getting an error .If i change the code to the below it will work fine

我的根文件夹有几个文件夹,其中包含一些嵌套的内部文件夹。当我在 Appcode 类中调用这段代码时,从第一级文件夹开始,我能够正确加载 XML 文件,因为路径是正确的。现在如果我从内部文件夹调用同一段代码,我收到一个错误。如果我将代码更改为下面的代码,它将正常工作

xmlSiteConfig.Load(System.Web.HttpContext.Current.Server.MapPath("../../myConfig.xml"));

How can i solve this.I dont want to change the file path for various calls to this code.With what piece of code I can solve the issue so that the program will load the XML file irrespective of the calling position . Any advice ?

我该如何解决这个问题。我不想更改对此代码的各种调用的文件路径。我可以用什么代码来解决问题,以便程序将加载 XML 文件,而不管调用位置如何。有什么建议吗?

Thanks in advance

提前致谢

采纳答案by David M

If it's in the root folder, use this:

如果它在根文件夹中,请使用:

Server.MapPath("~/myConfig.xml")

This will work from any directory.

这将适用于任何目录。

回答by Ian

Prefix your path string with a tilde (~) - this represents the root of the website:

用波浪号 (~) 前缀您的路径字符串 - 这代表网站的根:

xmlSiteConfig.Load(System.Web.HttpContext.Current.Server.MapPath("~/myConfig.xml"));

回答by Tommy

Does Server.MapPath("~/xmlFile.xml") work for you? The ~/ tells the .NET application to always start from the application root. If your XML file is in a sub folder (not where you are calling the function but the actual physical file), then you would use server.mappath("~/myfolder/xmlFile.xml").

Server.MapPath("~/xmlFile.xml") 对你有用吗?~/ 告诉 .NET 应用程序总是从应用程序根启动。如果您的 XML 文件位于子文件夹中(不是您调用函数的位置,而是实际的物理文件),那么您将使用 server.mappath("~/myfolder/xmlFile.xml")。

回答by Edwin Tai

the method System.Web.HttpContext.Current.Server.MapPath("") is to get the root path of the web app. so System.Web.HttpContext.Current.Server.MapPath("../myConfig.xml") is to get the father path of the web app it is wrong if your file is not here. you can use System.Web.HttpContext.Current.Server.MapPath("/path") instead.

System.Web.HttpContext.Current.Server.MapPath("") 方法是获取 Web 应用程序的根路径。所以 System.Web.HttpContext.Current.Server.MapPath("../myConfig.xml") 是为了获取 web 应用程序的父路径,如果你的文件不在这里,这是错误的。您可以改用 System.Web.HttpContext.Current.Server.MapPath("/path") 。

in other hand,you can use "~" to mean the root path in some asp.net control.

另一方面,您可以使用“~”来表示某些 asp.net 控件中的根路径。

回答by Chirag Khatsuriya

Server.MapPath("~/MYXML.xml")

In this "~" means, root directory. If you want to find in any sub directory then you should give path e.g,

这里的“~”表示根目录。如果你想在任何子目录中查找,那么你应该给出路径,例如,

Server.MapPath("~/App_Data/MyXml.xml")