Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access IApplicationEnvironment.ApplicationBasePath

Tags:

asp.net-core

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?

like image 839
ygoe Avatar asked May 18 '16 11:05

ygoe


People also ask

How do I find my web root path?

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.

How do I get wwwroot path in .NET core?

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.

What is content root path?

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.

Where is IWebHostEnvironment?

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.


2 Answers

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.

like image 57
Tseng Avatar answered Sep 23 '22 17:09

Tseng


Alternatively, use

AppContext.BaseDirectory

like image 31
lxa Avatar answered Sep 22 '22 17:09

lxa