ASP.NET MVC - 从控制器中查找 App_Data 文件夹的绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1268738/
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
ASP.NET MVC - Find Absolute Path to the App_Data folder from Controller
提问by BuddyJoe
What is the correct way to find the absolute path to the App_Data folder from a Controller in an ASP.NET MVC project? I'd like to be able to temporarily work with an .xml file and I don't want to hardcode the path.
从 ASP.NET MVC 项目中的控制器找到 App_Data 文件夹的绝对路径的正确方法是什么?我希望能够临时使用 .xml 文件,但我不想对路径进行硬编码。
This does not work:
这不起作用:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
string path = VirtualPathUtility.ToAbsolute("~/App_Data/somedata.xml");
//.... do whatever
return View();
}
}
I think outside of the web context VirtualPathUtility.ToAbsolute() doesn't work. string path comes back as "C:\App_Data\somedata.xml"
我认为在 Web 上下文之外 VirtualPathUtility.ToAbsolute() 不起作用。字符串路径返回为“C:\App_Data\somedata.xml”
Where should I determine the path of the .xml file in an MVC app? global.asax and stick it an application-level variable?
我应该在哪里确定 MVC 应用程序中 .xml 文件的路径?global.asax 并将其粘贴为应用程序级变量?
回答by eu-ge-ne
ASP.NET MVC1 -> MVC3
ASP.NET MVC1 -> MVC3
string path = HttpContext.Current.Server.MapPath("~/App_Data/somedata.xml");
ASP.NET MVC4
ASP.NET MVC4
string path = Server.MapPath("~/App_Data/somedata.xml");
MSDN参考:
回答by Alex
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
This is probably a more "correct" way of getting it.
这可能是一种更“正确”的获取方式。
回答by Simon_Weaver
I try to get in the habit of using HostingEnvironmentinstead of Serveras it works within the context of WCF services too.
我尝试养成使用HostingEnvironment而不是Server因为它在 WCF 服务的上下文中工作的习惯。
HostingEnvironment.MapPath(@"~/App_Data/PriceModels.xml");
回答by Daniel Lidstr?m
The most correct way is to use HttpContext.Current.Server.MapPath("~/App_Data");. This means you can only retrieve the path from a method where the HttpContextis available. It makes sense: the App_Data directory is a web project folder structure [1].
最正确的方法是使用HttpContext.Current.Server.MapPath("~/App_Data");. 这意味着您只能从HttpContext可用的方法中检索路径。这是有道理的:App_Data 目录是一个 web 项目文件夹结构 [1]。
If you need the path to ~/App_Data from a class where you don't have access to the HttpContextyou can always inject a provider interface using your IoC container:
如果您需要从无法访问的类中获取 ~/App_Data 的路径,HttpContext您可以随时使用 IoC 容器注入提供程序接口:
public interface IAppDataPathProvider
{
string GetAppDataPath();
}
Implement it using your HttpApplication:
使用您的HttpApplication:
public class AppDataPathProvider : IAppDataPathProvider
{
public string GetAppDataPath()
{
return MyHttpApplication.GetAppDataPath();
}
}
Where MyHttpApplication.GetAppDataPathlooks like:
哪里MyHttpApplication.GetAppDataPath看起来像:
public class MyHttpApplication : HttpApplication
{
// of course you can fetch&store the value at Application_Start
public static string GetAppDataPath()
{
return HttpContext.Current.Server.MapPath("~/App_Data");
}
}
[1] http://msdn.microsoft.com/en-us/library/ex526337%28v=vs.100%29.aspx
[1] http://msdn.microsoft.com/en-us/library/ex526337%28v=vs.100%29.aspx
回答by Rudy Lattae
Phil Haak has an example that I think is a bit more stable when dealing with paths with crazy "\" style directory separators. It also safely handles path concatenation. It comes for free in System.IO
Phil Haak 有一个例子,我认为在处理带有疯狂“\”样式目录分隔符的路径时,它会更稳定一些。它还可以安全地处理路径串联。它在 System.IO 中免费提供
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
However, you could also try "AppDomain.CurrentDomain.BaseDirector" instead of "Server.MapPath".
但是,您也可以尝试“AppDomain.CurrentDomain.BaseDirector”而不是“Server.MapPath”。
回答by Dipak Delvadiya
string filePath = HttpContext.Current.Server.MapPath("~/folderName/filename.extension");
OR
或者
string filePath = HttpContext.Server.MapPath("~/folderName/filename.extension");
回答by Shahbaz Pirzada
string Index = i;
string FileName = "Mutton" + Index + ".xml";
XmlDocument xmlDoc = new XmlDocument();
var path = Path.Combine(Server.MapPath("~/Content/FilesXML"), FileName);
xmlDoc.Load(path); // Can use xmlDoc.LoadXml(YourString);
this is the best Solution to get the path what is exactly need for now
这是获得现在真正需要的路径的最佳解决方案

