In the software development word, it is very common to have multiple different environments for the same application. An application normally has at least three environments: Development, Staging, and Production. ASP.NET Core helps us to differentiate application behavior for each environment based on the project configuration file.
In this post, we will see how to make the application know which configuration file to use, based on the environment variable ASPNETCORE_ENVIRONMENT.
ASP.NET allows us to differentiate application behavior based on different versions of the project configuration file. ASP.NET Core applications are not different. By default, when we create a new web project on Visual Studio 2017, the project carries a file called appsettings.json. Another file is also created as complement, e.g. appsettings.Development.json.
An ASP.NET Core project can have as many configuration files as necessary to differentiate the many environments you have. The environment variable will let the application knows (-S) which is the correct appsettings.json to use, though. When publishing a package, all configuration files will be automatically copied to the package. The environment variable configured on the server differentiates which one the application will use.
Using the debug mode, inside of the Visual Studio, it is possible to emulate which variable will be used. We just need to create a new environment name called ASPNETCORE_ENVIRONMENT and input the value we want to use.
In this case, the file that will be used for the ASP.NET Core application will be the appsettings.Development.json. By default, there is no ASPNETCORE_ENVIRONMENT variable created or the value does not match with the file name. For these cases, the default file will be used (appsettings.json).
In a windows server, it is possible to define the environment variable in the session environment variables.
For this case, the program will use the file appsettings.Prodution.json.
It is also possible to define the environment variable in an Azure application, in the session application settings inside of your service.
ASP.NET Core uses the variable ASPNETCORE_ENVIRONMENT whenever the application is initialized. It is possible to check which is the correct environment using the IHostingEnvironment interface.
By default, there are three environments pre-configured: Development, Staging, and Production.
It is possible to create and use your own environment and use the method IsEnvironment(“YOUR_NEW_ENVIRONMENT”). This way ASP.NET allows us to manage the application behavior based on environment variables.
Having all the environments configurated with the correct environment variable is one of the easiest ways to manage multiple environments. After having that, you don’t need to worry about the confirmation file, if all of them were correctly configured previously.
About the author
Luis Eduardo Lanfredi is a Software Engineer at Poatek. He holds a BSc in Information Systems. Most of the time he works with Microsoft Technologies and web applications.