All About Appsettings.json In ASP.NET Core 2.0

As most of you are aware, in ASP.NET Core, we don't have anything called Web.config which we use to write our connection strings or application specific settings. Rather, here we have a file named appsettings.json to store similar information. There are a few very common use cases where we need to maintain multiple appsettings.json files in a single solution. For example,

  • Multiple application settings per application
    When we need to maintain different-different application-specific settings based on the application environment. Say, one can have one type of application settings for Development, another type of application settings for Production, another one for Staging, and so on. Needless to mention, all the appsettings files will have different names.

  • To implement inheritance
    If there are some common settings between multiple application settings file, the developer can come up with a base application settings file and on top of that specific file can be created. In that case, common information need not to be repeated in all the files.
If you are aware of the ASP.NET Core architecture, then you must agree on the point that such scenarios can also be handled very easily in the ASP.Net Core as plugging in multiple sources is very straightforward.

So, coming to the point. My article is all about how to inherit or read data from an appsettings.json file which is outside of my project. This scenario usually comes into the picture when we are maintaining different projects for holding all the shared resources that have to be consumed across many projects in a given solution file. So, in the real world, one project can have its project specific settings as well as some common (or say global) settings which are placed outside its boundary.

Let me first tell you something about my project structure. Here goes my solution structure:

 

As you can see in the above figure, 'CommonSettings.json' is the file which is kept outside of the main project named 'AllAboutConfigurations' and here is how my both the JSON files look like.

appsettings.json
  1. {  
  2.   "Logging": {  
  3.                 "IncludeScopes"false,  
  4.                 "LogLevel": { "Default""Warning" }  
  5.              },  
  6.   
  7.   "MySettings": {   
  8.                   "PersonalizedSettings"" It's just for me" 
  9.                 }   
  10. }  
CommonSettings.json
  1. {  
  2.   "MySettings": {   
  3.                          "CommonSettings""Hi, I'm common setting. Anyone can use me." 
  4.                 }  
  5. }  
Now, in order to read 'CommonSettings.json' in 'AllAboutConfigurations' project, I have to update the application configuration while constructing the web host as shown below.
  1. public static IWebHost BuildWebHost(string[] args) =>  
  2.             WebHost.CreateDefaultBuilder(args)  
  3.                 .ConfigureAppConfiguration((webHostBuilderContext, configurationbuilder) =>  
  4.                 {  
  5.                     var environment = webHostBuilderContext.HostingEnvironment;  
  6.                     string pathOfCommonSettingsFile = Path.Combine(environment.ContentRootPath,"..","Common");  
  7.                     configurationbuilder  
  8.                             .AddJsonFile("appSettings.json", optional: true)  
  9.                             .AddJsonFile(Path.Combine(pathOfCommonSettingsFile, "CommonSettings.json"), optional: true);  
  10.   
  11.                     configurationbuilder.AddEnvironmentVariables();  
  12.                 })  
  13.                 .UseStartup()  
  14.                 .Build();  
Now, if we want to see something on our web page, we have to update UI code too. Let's make it simple with just a few lines of code as shown below.
  1. @using Microsoft.Extensions.Configuration;  
  2. @inject IConfiguration configuration;  
  3.   
  4. @{  
  5.     Layout = null;  
  6. }  
  7.   
  8.   
  9. <html>  
  10. <head>  
  11.     <title>Settings</title>  
  12. </head>  
  13. <body>  
  14.         Personalized Settings: @configuration.GetSection("MySettings")["PersonalizedSettings"]<br />   
  15.         Common Settings: @configuration.GetSection("MySettings")["CommonSettings"]<br />  
  16. </body>  
  17. </html>  

If you will run your application now, you will be able to see that both the settings are considered as part of a single project.

 
Happy Learning!

Future references

Andrew Lock's post and MS Build 2017.