- In the previous versions of ASP .NET, any configuration setting or parameter you needed was added in web.config file
- In ASP.NET Core, Configuration settings can be different configuration sources like:
- Command-line arguments
- Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)
- Environment variables
- In-memory .NET objects
- Custom configuration sources
- App configuration in ASP.NET Core is based on key-value pairs established by configuration providers
appsettings.json file
- appsettings.json file is located inside the root directory of the project, as you can see on below figure.
- Configuration settings in ASP.NET Core usually stores in key-value pairs
- Let me show you how to add a new key in appsettings.json file. I have added a key with name "keyMessage", as shown below:
- To access Configuration information in the Startup class, inject the IConfiguration service provided by the Framework and used this object to read the specific key from appsettings.json
- Here, in the above figure, you can see that I am using Dependency Injection. Dependency Injection is an integral part in ASP.NET Core
- Now lets run the application and then you will see the output as "This message is from appsettings.json file".
Important points to know about IConfiguration service in ASP.NET Core
- IConfiguration service is setup to read configuration information
- Web apps based on the ASP.NET Core call WebHost.CreateDefaultBuilder method which automatically invoked when application starts. CreateDefaultBuilder provides default configuration for the app.
- Default App configuration is provided in the following order:
- Here, the order means that the later configuration sources override the earlier configuration sources
- appsettings.json - using File Configuration Provider
- appsettings.{Environment}.json - using File Configuration Provider
- User Secrets - using Secret Manager (in the Development environment only)
- Environment variables - using the Environment Variables Configuration Provider
- Command-line arguments - using the Command-line Configuration Provider
- If there is a configuration setting with the same key in multiple configuration sources, the later configuration sources override the earlier configuration sources
- Now as for an example, I have setup appsettings.json file and appsettings.Development.json with key "keyMessage" using different values, as shown in figure below
- Now run th application, you will see the output that the key value of appsettings.json file is overridden by appsettings.Development.json file
- Similarly, add the same key on environment variables(in launchsettings.json file)
- Now run the application using IIS Express profile(as new key is added to the Development environment of IIS Express profile as shown in figure above) & see the output:
- Here you can see the output that, the key value of appsettings.Development.json file is overriden by the key value of environment variables.
- Similarly, add the same key using Command-line and press enter
- Now navigate to your browser and refresh the URL
- You can see the output of command-line configuration source. Here all other sources are overridden by command-line source
- You can change this order if you want to or even add your own custom configuration sources in addition to all the existing configuration sources.
Nice article , its really helpful
ReplyDelete