Reputation: 4811
I am building a console app and i have to save a couple of settings values and some of them is an array of values A sample section of Appsettings i am thinking is like below
"FolderSettings":{
"source": "c:\\sourcefolder",
"target": "c:\\targetfolder"
},
"FolderJPG":[
{
"name":"foldername",
"width":1450,
"height":1450
}
],
"FolderPNG":[
{
"name":"foldername",
"width":300,
"height":300
},
{
"name":"foldername2",
"width":450,
"height":450
}
]
Is it recommended to save the array of settings values in appsettings file [ FolderJPG and FolderPNG in this case ] or is there any recommended ways to keep such settings in .net core?
Also, how can i fetch the settings values as an array of values? I know the way to read a simple key-value pair will be like
_configuration.GetValue<string>("FolderSettings:source");
But how can I read the array of settings values for FolderJPG, FolderPNG etc correctly ?
Upvotes: 0
Views: 348
Reputation: 24569
Bind it to the model
public class FolderOption
{
public string Name { get; set; }
public int Width{ get; set; }
public int Height{ get; set; }
}
Then
_configuration.GetSection("FolderJPG").Get<List<FolderOption>>();
The appsettings.json can contain any configuration that you need with restriction - it should be JSON.
JSON objects are written in key/value pairs.
Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
So, an array is just data type of value
Upvotes: 2