Sunday 23 May 2021

Part 9 : Accessing Values From Different Configuration

#5 minutes of reading

In this article I will show you, how you can configure properties like configuring properties in web.config in old asp.net framework. Here in Asp.Net core we don’t have web.config but core support configuring properties from multiple files like AppSetting.Json, Environment variable, User secrete, command line argument and custom file created to store configuration. Will check all different configuration in asp.net core one by one. Let’s start with appsettings.json.
I have created web API project shown below and created Configurationcontroller.
Open appsettings.json file and add config property shown below.
Open ConfigController.cs file and Inject IConfiguration interface which is responsible for reading config properties from different config sources.
For now I’m reading value from appSettings.json later will show you how to read value from User secrete, environment variable and custom file.
For user secret, right click on project-> Manage User Secrets and add config property shown below. User secrets works on development environment.
For environment variable, right click on project -> properties->Debug, shown highlighted in below screen.
Run application and check response.
I have added below highlighted keys in appSettings.json
Implement below Http get method which return list of available config properties under Configuration controller.
In above screen, API return all configuration key available and to get specific key value from API, call get method with key name shown below.
I shown you asp.net core provide variety of different files for configuration. Advantage of this is if you are in development environment you can configure connection string in User secret or environment variable, for staging and propduction connection string(environment variable) should be different.
Core also provide concept of environment based files. You can create appSettings.{Environment}.Json. based on Environment specific appSetting file used for reading configured values.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home