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.
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.
The App_Data folder can contain application data files like LocalDB, . mdf files, XML files, and other data related files.
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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With