Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net vNext App_Data folder

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.

like image 540
Andrew Van Den Brink Avatar asked Dec 10 '14 11:12

Andrew Van Den Brink


People also ask

What is App_Data folder in asp net?

App_Data contains application data files including . mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information.

Which files should be placed in App_Data folder?

The App_Data folder can contain application data files like LocalDB, . mdf files, XML files, and other data related files.

What is the use of App_Data folder in MVC?

The App_Data folder of MVC application is used to contain the application related data files like . mdf files, LocalDB, and XML files, etc. The most important point that you need to remember is that IIS is never going to serve files from this App_Data folder.

What is App_Data used for?

The AppData folder includes application settings, files, and data unique to the applications on your Windows PC. The folder is hidden by default in Windows File Explorer and has three hidden sub-folders: Local, LocalLow, and Roaming. You won't use this folder very often, but this is where your important files reside.


1 Answers

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();
}
like image 183
Mihai Dinculescu Avatar answered Oct 13 '22 00:10

Mihai Dinculescu