In my appsettings.test.json I have the following structure.
{
"ViewOptionsConfiguration": {
"Default": {
"GroupingOptions": [
{ "Key": "SortKey1", "Label": "SortVal1" },
{ "Key": "SortKey2", "Label": "SortVal2" }
]
}
}
}
Then, I can read it using the following syntax (obtaining correct values).
string path = "ViewOptionsConfiguration:Default:GroupingOptions";
string key0 = config.GetValue<string>(path + ":0:Key")
string lab0 = config.GetValue<string>(path + ":0:Label")
string key1 = config.GetValue<string>(path + ":1:Label")
string lab1 = config.GetValue<string>(path + ":1:Label")
Now, I introduced a class ViewOption
like so, with intention of mapping the config to an array of such elements.
public class ViewOption
{
public string Key { get; set; }
public string Label { get; set; }
}
I was expecting that the following would work. Regrettably, it seems that the mapped value is null
, so I guess the mapping fails recognizing the values.
ViewOption option0 = config.GetValue<ViewOption>(path + ":0")
List<ViewOption> options = config.GetValue<List<ViewOption>>(path)
What am I missing in the mapping?
My workaround is doing something like the below. However, it's ugly as duck and, also, I need to figure out the number of elements for the array separately, which is even uglier and duckier.
ViewOption option0 = new ViewOption(
config.GetValue<string>(path + ":0:Key"),
config.GetValue<string>(path + ":0:Label"));