ASP.NET MVC - Finden Absoluten Pfad zum Ordner App_Data von Steuer
-
13-09-2019 - |
Frage
Was ist der richtige Weg, um den absoluten Pfad zu dem Ordner App_Data aus einer Steuerung in einem ASP.NET MVC-Projekt zu finden? Ich möchte in der Lage sein, vorübergehend mit einer XML-Datei zu arbeiten, und ich will nicht den Pfad codieren.
Das funktioniert nicht:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
string path = VirtualPathUtility.ToAbsolute("~/App_Data/somedata.xml");
//.... do whatever
return View();
}
}
Ich denke, außerhalb des Web-Kontext VirtualPathUtility.ToAbsolute () funktioniert nicht. String-Pfad kommt zurück als "C: \ App_Data \ somedata.xml"
Wo soll ich den Pfad der XML-Datei in einem MVC-app bestimmen? Global.asax und kleben Sie es ein Application-Level-Variable?
Lösung
ASP.NET MVC1 -> MVC3
string path = HttpContext.Current.Server.MapPath("~/App_Data/somedata.xml");
ASP.NET MVC4
string path = Server.MapPath("~/App_Data/somedata.xml");
MSDN Referenz:
Andere Tipps
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
Dies ist wahrscheinlich ein „richtiger“ Weg, um es zu bekommen.
Ich versuche, in der Gewohnheit, der Verwendung von HostingEnvironment
statt Server
, wie es auch im Rahmen der WCF-Dienste funktioniert.
HostingEnvironment.MapPath(@"~/App_Data/PriceModels.xml");
Der richtige Weg ist HttpContext.Current.Server.MapPath("~/App_Data");
zu verwenden. Das heißt, Sie können nur den Weg von einer Methode abzurufen, wo die HttpContext
zur Verfügung steht. Es macht Sinn: das Verzeichnis App_Data ist eine Webprojekt Ordnerstruktur [1]
Wenn Sie den Pfad zu ~ / App_Data aus einer Klasse, wo Sie haben keinen Zugriff auf den HttpContext
Sie immer eine Provider-Schnittstelle mit Ihrem IoC Behälter injizieren:
public interface IAppDataPathProvider
{
string GetAppDataPath();
}
Implementieren Sie Ihre HttpApplication
mit:
public class AppDataPathProvider : IAppDataPathProvider
{
public string GetAppDataPath()
{
return MyHttpApplication.GetAppDataPath();
}
}
Wo MyHttpApplication.GetAppDataPath
wie folgt aussieht:
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
Phil Haak hat ein Beispiel, das ich denke, ein bisschen mehr stabil ist, wenn mit Pfaden mit verrückten „\“ style Verzeichnis Separatoren handeln. Es behandelt auch sicher Pfad Verkettung. Es kommt kostenlos in System.IO
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
Sie können jedoch auch versuchen, "AppDomain.CurrentDomain.BaseDirector" statt "Server.MapPath".
string filePath = HttpContext.Current.Server.MapPath("~/folderName/filename.extension");
oder
string filePath = HttpContext.Server.MapPath("~/folderName/filename.extension");
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);
Das ist die beste Lösung, den Weg zu bekommen, was jetzt genau brauchen