ASP.Net vNext App_Data Folder

A similar question found here: ASP.NET MVC - find the absolute path to the App_Data folder from the controller.

App_Data folder gone? Server.MapPath seems to be gone too.

I tried to achieve the same results using Url.Content , but it does not seem to work.

+7
c # asp.net-mvc asp.net-core
source share
2 answers

We have App_Data in vNext.

This should work

 string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); 

As for Server.MapPath equivalents, you can use AppDomain.CurrentDomain.BaseDirectory and build your way from there.

You can also use the IApplicationEnvironment service

 private readonly IApplicationEnvironment _appEnvironment; public HomeController(IApplicationEnvironment appEnvironment) { _appEnvironment = appEnvironment; } public IActionResult Index() { var rootPath = _appEnvironment.ApplicationBasePath; return View(); } 

IHostingEnvironment is the moral equivalent of IApplicationEnvironment for web applications . For PhysicalFileSystem, IHostingEnvironment returns to IApplicationEnvironment .

 private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public IActionResult Index() { var rootPath = _hostingEnvironment.MapPath("APP_DATA"); return View(); } 
+8
source share

MapPath exists in IHostingEnvironment

 private readonly IHostingEnvironment _env; public HomeController(IHostingEnvironment env) { _env = env; } public IActionResult Index() { var dataFolderPath = _env.MapPath("APP_DATA"); return View(); } 
+4
source share

All Articles