Access IApplicationEnvironment.ApplicationBasePath

How can I access the base path of an ASP.NET base application from the IApplicationEnvironment.ApplicationBasePath property 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 directory. There is also no HttpContext, because the method will be called regardless of requests by the timer. Can I ask a dependency container or something for this information? Is there any other way to get this information?

+8
asp.net-core
source share
2 answers

Always check out ads on GitHub for beta.

As announced here , the IApplicationEnvrionment interface IApplicationEnvrionment 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 use the PlatformServices.Default static method to access specific types or register it in an IoC container and enable it elsewhere. Later preferable to most customs.

+10
source share

now you can get it from

 IHostingEnvironment.ContentRootPath 

IApplicationEnvironment no longer exists

+7
source share

All Articles