类似的问题发现在这里:
ASP.NET MVC – Find Absolute Path to the App_Data folder from Controller.
App_Data文件夹消失了吗? Server.MapPath似乎也消失了.
我试图用Url.Content实现相同的结果,但它似乎没有起作用.
解决方法
我们在vNext中有App_Data.
这应该仍然有效
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
对于Server.MapPath等价物,您可以使用AppDomain.CurrentDomain.BaseDirectory并从那里构建路径.
您还可以使用IApplicationEnvironment服务
private readonly IApplicationEnvironment _appEnvironment; public HomeController(IApplicationEnvironment appEnvironment) { _appEnvironment = appEnvironment; } public IActionResult Index() { var rootPath = _appEnvironment.ApplicationBasePath; return View(); }
IHostingEnvironment是Web应用程序的IApplicationEnvironment的道德等价物.对于PhysicalFileSystem,IHostingEnvironment回退到IApplicationEnvironment.
private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public IActionResult Index() { var rootPath = _hostingEnvironment.MapPath("APP_DATA"); return View(); }