In this article we will discuss about ASP.NET Core appsettings.json file. If you miss our previous article then please follow the below link ASP .NET Core launchSettings.json file. This article will cover below points.
What is ASP.NET Core appsettings.json File ?
When we create an asp.net core web application with an Empty project template, then the visual studio automatically creates the appsettings.json file for us as shown in the below image.
Lets add a key named as myKey within this file. To do so, modify the appsettings.json file as shown below. As it is a JSON file, you need to store the value in the form of key-value pair.
- What are the different Configuration Sources available in the ASP.NET Core application?
- What is the ASP.NET Core appsettings.json file?
- How to access the configuration information in ASP.NET Core Application?
- What is the Configuration Execution Order in ASP.NET Core Application?
- What is the Default Orders of reading the configuration sources?
- How to Pass Config value from Command Line in ASP.NET Core Application?
What are the different Configuration Sources available in the ASP.NET Core application ?
If you have worked with the previous versions of the ASP.NET application, then you make know the importance of the web.config file. In previous versions of ASP.NET application, we generally used to store the application configuration settings such as database connection strings, any application scope global variables and many more within the web.config file.
But in ASP.NET Core, the application configuration settings can come from different configurations sources such as
- Files (appsettings.json, appsettings.{Environment}.json, where the {Environment} is the nothing but the applications current hosting environments such as Development, Staging or Production)
- User secrets
- Environment variables
- Command-line arguments
When we create an asp.net core web application with an Empty project template, then the visual studio automatically creates the appsettings.json file for us as shown in the below image.
Lets add a key named as myKey within this file. To do so, modify the appsettings.json file as shown below. As it is a JSON file, you need to store the value in the form of key-value pair.
How to access the configuration information in the ASP.NET Core application ?
To access the configuration information within the Startup class, you need to use the IConfiguration service which is provided by the ASP.NET Core Framework. So what you need to do is just inject the IConfiguration service through the constructor of the Startup class. To do so modify the Startup class which is present in the Startup.cs file as shown below.
Explanation of the above code:
First, we created a private variable of type IConfiguration. Then through constructor dependency injection we inject the IConfiguration object and store it within the private variable. The following code exactly does this.
Then we access the configuration variable i.e. myKey using the IConfiguration service instance. The following piece of code exactly does the same thing.
Set the AspNetCoreHostingModel value to InProcess in the application’s project file as shown below.
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
Now run the application and you see the value as expected in the browser window as shown in the below image.
What is the Configuration Execution Order in ASP.NET Core Application ?
Before understanding the execution order, let’s have a look at the appsettings.Development.json file. You can find this file within the appsettings.json file with modified json value as shown below.
When we run the application and see the result.
As you can see we are using the same key as we use in the appsettings.json file. Now run the application and see the output as shown below.
What is the Default Orders of reading the configuration sources ?
The default orders in which the various configuration sources are read for the same key are as follows
- appsettings.json,
- appsettings.{Environment}.json here we use appsettings.development.json
- User secrets
- Environment variables
- Command-line arguments
Now we already have myKey in two places i.e. appsettings.json and appsettings.development.json. Now add the same key as “myKey”: “myKeyValue coming from Environment Variable of launchsSettings.json” in the IIS Express profile section of the launchSettings.json file as shown below.
With this change now run the application and it should display the value coming from the environment variable.
How to Pass Config value from Command Line in ASP.NET Core Application ?
You can in above when we pass the the key value in CLI then same value is printed irrespective of value that are in declared in appsettings.json.
Summary
Post a Comment