Introduction
In this post we will explain how to specify the AspNetCore Environment Variable at IIS-level. We can setup the Environment variable 'ASPNETCORE_ENVIROMENT' and run more than one environment instances on the same machine.
For example, for your application/API (lets call it WebApi) you can install two instances on the same machine, first instance for testing environment and second instance for staging environment.
To elaborate it more, refer below code snippet where in, in the Startup.cs file's Configure method, by using input parameter IWebHostEnvironment, we can execute specific code based on the environment. In this example, if it is development environment then we will display 'developer exception page', which will provide developer the needed details to debug the issue. But if it is a production environment then we are making sure that our application always uses HTTPS.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
else if (env.IsProduction()) {
// do something specific to Production instance.
app.UseHsts();
app.UseHttpsRedirection();
}
// Rest of the code
. . .
}
Steps to follow:
1. Open IIS (Run >> inetmgr) and after selecting your web application, open the 'Configuration Editor' as shown in below image.
2. Once the 'Configuration Editor' is open select the value 'system.webServer' >> 'aspNetCore' from the 'Section' dropdown, as show in below image.
3. On the same screen for the second dropdown i.e. 'From', choose the third option i.e. 'ApplicationHost.Config', as demonstrated in below image.
4. Click the third field 'environmentVariables', you can open this collection by click on the ellipses (i.e. three dots '...') button, as specified in the below image.
5. Clicking the ellipses button will open a 'Collection Editor', from right-hand side 'Action' choose 'Add' link, and specify the values for 'name' and 'value' fields. In this case for the 'name' you will add value as 'ASPNETCORE_ENVIROMENT', and for the 'value' type 'Production'.
Summary
We have explained, step-by-step how to setup the Environment Variable - 'ASPNETCORE_ENVIROMENT' to setup more than one environment instance on the same machine, at IIS-level.
Your feedback
Your feedback is of paramount importance to us, provide your valuable inputs; what else you would like to read about, or if you would like the author to elaborate on any specific topic from this article, or any other article on this blog.
Disclaimer
The material presented in this blog is to help educate the developers, and the author(s) & owner(s) of this blog are not responsible of any damage arising out of use of the code snippets or any other contents of this blog.
Comments
Post a Comment