7

Similar question to the one found here: ASP.NET MVC - Find Absolute Path to the App_Data folder from Controller.

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

I tried to achieve the same results with Url.Content, but it doesn't seem to be working.

Community
  • 1
  • 1

2 Answers2

8

We do have App_Data in vNext.

This should still work

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

As for Server.MapPath equivalents you can use AppDomain.CurrentDomain.BaseDirectory and build your path 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 the IApplicationEnvironment for web applications. For PhysicalFileSystem, IHostingEnvironment falls back to IApplicationEnvironment.

private readonly IHostingEnvironment _hostingEnvironment;

public HomeController(IHostingEnvironment hostingEnvironment)
{
    _hostingEnvironment = hostingEnvironment;
}

public IActionResult Index()
{
   var rootPath = _hostingEnvironment.MapPath("APP_DATA");
   return View();
}
Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
  • config.json as of vs2015 preview version is stored at project folder, i'm not sure if we can call that App_Data folder. I just found out that we can also use IHostingEnvironment.WebRoot within Startup class. will this be official best practice or still workaround? – Andrew Van Den Brink Dec 11 '14 at 06:03
  • 1
    It looks like they have changed it's location. IHostingEnvironment is the moral equivalent of the IApplicationEnvironment but for web applications. For PhysicalFileSystem, IHostingEnvironment falls back to IApplicationEnvironment. – Mihai Dinculescu Dec 11 '14 at 07:24
  • 4
    string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); will not work on CoreCLR – davidfowl Jul 04 '15 at 19:40
4

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();
}
Vitaly
  • 2,064
  • 19
  • 23