App Settings File According To Environment Variable .Net Core API

Why do we need this?

 
In a real-world application, we normally have multiple environments for our applications such as development, staging, production, etc. and according to these environments, we have multiple values for the same keys. A primary example of this is we usually have different databases for different environments. So, this article will tell you how can you use environment based appsettings file without having to change the values manually for each environment.
 
Let’s get started.
 
Create a project in visual studio for ASP.NET Core API,
 
App Settings File According To Environment Variable .Net Core API
 
App Settings File According To Environment Variable .Net Core API
 
App Settings File According To Environment Variable .Net Core API
 
After these steps, your project will be created and it will look something like this:
 
App Settings File According To Environment Variable .Net Core API
 
If you expand appsettings.json you will see appsettings.Development.json. DotNet core automatically creates this file for you
 
App Settings File According To Environment Variable .Net Core API
 
Now we will add a section in appsettings.json.
  1. "MySettings": {  
  2.     "DbConnection""my prod db connection",  
  3.     "Email""[email protected]",  
  4.     "SMTPPort""5605"  
  5. },   
Now we will add a section in appsettings.Development.json.
  1. "MySettings": {  
  2.     "DbConnection""my development db connection",  
  3.     "Email""[email protected]",  
  4.     "SMTPPort""5605"  
  5. }   
You see that these are the same sections but with different values. 
 
Now our appsettings.json file looks something like this:
 
App Settings File According To Environment Variable .Net Core API
 
Now our appsettings.Development.json file looks something like this:
 
App Settings File According To Environment Variable .Net Core API
 
Now we will add the code to read values from appsettings.json:
 
 
Now if you open Properties folder and open launchSettings.json and you will see the ASPNETCORE_ENVIRONMENT variable and its value is Development.
 
App Settings File According To Environment Variable .Net Core API
 
After adding the code to read appsettings values our controller looks something like this:
 
App Settings File According To Environment Variable .Net Core API
 
Now we will add another appsettings file for staging environment and we will name it as appsettings.Staging.json. Add a file by right-clicking on the project and click on add item.
 
App Settings File According To Environment Variable .Net Core API
 
Our project structure will look like this:
 
App Settings File According To Environment Variable .Net Core API
 
Now we will add MySettings section in this (appsettings.Staging.json) file also
  1. "MySettings": {  
  2.     "DbConnection""my staging db connection",  
  3.     "Email""[email protected]",  
  4.     "SMTPPort""5605"  
  5. }   
Now our appsettings.Staging.json will look like this.
 
App Settings File According To Environment Variable .Net Core API
 
Now at our controller, we will add a breakpoint to debug our code.
 
App Settings File According To Environment Variable .Net Core API
 
Now we will run our application, and since in launch settings our ASPNETCORE_ENVIRONMENT variable is Development so our code should read the value from development file.
 
App Settings File According To Environment Variable .Net Core API
 
We can see that our code is reading from the development file. 
 
Now let’s change our ASPNETCORE_ENVIRONMENT value to Staging.
 
App Settings File According To Environment Variable .Net Core API
 
Now let’s run it again.
 
App Settings File According To Environment Variable .Net Core API
 

Summary

 
In this article, we have seen how we can have multiple appsettings files and use them according to the ASPNETCORE_ENVIRONMENT variable.
 
If you wish to see the code please Click Here! 
 
Happy Coding.


Similar Articles