How can I access the ASP.NET Core application base path from the property IApplicationEnvironment.ApplicationBasePath
from anywhere in the code? I'm not in the Startup class but in some other helper class and I need to find the current application's directory from there. There's also no HttpContext because the method will be called independently from requests by a timer. Can I ask the dependency container or something for that information? Is there another way to get this information?
For the Grid, a website's root directory is the …/html folder. This is located in the file path /domains/example.com/html. The root directory can be viewed/accessed through File Manager, FTP, or SSH.
The path of the wwwroot folder is accessed using the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core.
The content root path is the absolute path to the directory that contains the application content files. The web root path is the absolute path to the directory that contains the web-servable application content files.
IWebHostEnvironment is included in the Microsoft. AspNetCore. Hosting package, you simply need to add it as a reference to your project by right clicking on your project file and selecting 'Manage Nuget Packages' then searching for Microsoft. AspNetCore.
Always check the announcements on GitHub for the beta/rc releases.
As announced here, the IApplicationEnvrionment
interface has been removed from ASP.NET Core RC2.
To get the path in the Startup method, do this
public Startup(IHostingEnvironment hostingEnvironment)
{
var builder = new ConfigurationBuilder()
.SetBasePath(hostingEnvironment.ContentRootPath)
...
}
If you need it outside, you can either use the static PlatformServices.Default
method to access it's concrete types or register it with the IoC container and resolve it elsewhere. Later one is preferable for most usages.
Alternatively, use
AppContext.BaseDirectory
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