Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a list from appsettings.json in .net core

I have an appsettings.json file which looks like this:

{     "someSetting": {         "subSettings": [             "one",             "two",             "three"          ]     } } 

When I build my configuration root, and do something like config["someSetting:subSettings"] it returns null and the actual settings available are something like this:

config["someSettings:subSettings:0"]

Is there a better way of retrieving the contents of someSettings:subSettings as a list?

like image 932
devlife Avatar asked Aug 26 '16 15:08

devlife


People also ask

How Appsettings json works in .NET Core?

json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information. Actually, in ASP.NET Core, the application configuration settings can be stored in different configurations sources such as appsettings.


1 Answers

Assuming your appsettings.json looks like this:

{   "foo": {     "bar": [       "1",       "2",       "3"     ]   } } 

You can extract the list items like so:

Configuration.GetSection("foo:bar").Get<List<string>>() 
like image 107
Kirill Rakhman Avatar answered Oct 25 '22 08:10

Kirill Rakhman