Read Config Value Of Array Type In .NET Core

Introduction 

 
In most of our applications, we need some kind of configuration. When we have a fixed value and we know we might need to change it in future (i.e. connection string), we usually add it to our configuration file.
 
In the case of ASP.NET MVC, we usually add it to the web.config file. But with a .NET Core project, we will have to add it to the appsettings.json file. Here is an example of ASP.Net MVC config and .NET Core config. 
 
ASP.Net MVC 
  1. <configuration>    
  2.    <appSettings>    
  3.       <add key="apiUrl" value="http://localhost:2020"/>    
  4.    </appSettings>    
  5. </configuration>   
 .NET Core
  1. {  
  2.   "Logging": {  
  3.     "LogLevel": {  
  4.       "Default""Information",  
  5.       "Microsoft""Warning",  
  6.       "Microsoft.Hosting.Lifetime""Information"  
  7.     }  
  8.   },   
  9.   "apiUrl""http://localhost:2020"  
  10. }  
Here is the code to read config values in the server file:  (MVC controller / BLL / .NET Core controller).
 
ASP.NET MVC
  1. var apiUrl = ConfigurationManager.AppSettings["apiUrl"];  
 .NET core
  1. public class HomeController : Controller  
  2.   {  
  3.       public IConfiguration Config { getset; }  
  4.       public HomeController( IConfiguration config)  
  5.       {  
  6.           Config = config;  
  7.       }  
  8.       public IActionResult Index()  
  9.       {  
  10.           string apiConfig = this.Config["apiConfig"];  
  11.            
  12.           return View();  
  13.       }  
  14.   }  
Output
 
Read config value of array type in .Net core
 
As you can see, we can read our config value by simply defining the key in the config service.
 
Similarly, when we wish to store multiple values in config, how can we achieve it? Lets understand when do we need such scenario. Suppose we have a city dropdown in a online delivery site. This site works only for few cities as of now but in future it might operate in one or two more cities. Then how can we configure it?
 
Here we can use two approaches to solve the problem:
  1. We can define a configuration page in the application.
  2. We can define all cities as an array type in config. 
Let's discuss how to achieve it using the second approach. First, we should define all cities that we wish to display in our application. Here is the piece of code that we need to add to appsettings.json.
  1. {  
  2.   "Logging": {  
  3.     "LogLevel": {  
  4.       "Default""Information",  
  5.       "Microsoft""Warning",  
  6.       "Microsoft.Hosting.Lifetime""Information"  
  7.     }  
  8.   },  
  9.   "Cities": [ "City 1""City 2""City 3" ],  
  10.   "apiUrl""http://localhost:2020"  
  11. }  
Let's try to use our previous approach to read the config value.
 
 Read config value of array type in .Net core
 
Please ignore extra code. That's for my reference. 
 
As you can see, the value is null here. So, when we have value as array type, we need to add extra lines of code to read it. Here is the code.
  1. public IActionResult Index()  
  2.        {  
  3.            List<string> cities = this.Config.GetSection("Cities")?.GetChildren()?.Select(x => x.Value)?.ToList();  
  4.       
  5.            return View();  
  6.        }  
Here, we are trying to find the "Cities" section in the application config (appsettings.json), and then  GetChildren() method is used to read all children.
 
After that, we are trying to loop through each item and find its values by using the Select() extension method. Finally, use ToList() to convert our data list format so that we can directly bind it with the City dropdown. 
 
Here is the output:
 
 Read config value of array type in .Net core
 
As you can see we have all cities that are configured in appsettings.json in an action. Hope this will help you someday. Thanks for reading it.
 
Please feel free to post any suggestions or feedback.