I need to check in ConfigureServices
method whether the current hosting environment name is 'Development'.
So using IHostingEnvironment.IsDevelopment()
method may be ok for me, but unlike in Configure method, I do not have IHostingEnvironment env
.
If you need to check whether the application is running in a particular environment, use env. IsEnvironment("environmentname") since it will correctly ignore case (instead of checking if env. EnvironmentName == "Development" for example).
Fortunately for us, the answer is simple. You can add it to your Startup constructor: public Startup(IConfiguration configuration, IHostingEnvironment environment) . This will inject an IHostingEnvironment that you can set as a property available to any Startup method.
The IHostingEnvironment is an interface for . Net Core 2.0. The IHostingEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IHostingEnvironment interface have two properties.
The startup class contains two methods: ConfigureServices(): Registers the services that your application will need. Configure(): Configures the middleware pipeline that controls how the application processes the HTTP requests and sends the response.
just create a property in the Startup class to persist the IHostingEnvironment. Set the property in the Startup constructor where you already have access, then you can access the property from ConfigureServices
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