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

后端 未结 5 1883
耶瑟儿~
耶瑟儿~ 2020-12-24 00:13

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

{
    \"someSetting\": {
        \"subSettings\": [
            \"one\",
            \"two\",
                 


        
5条回答
  •  伪装坚强ぢ
    2020-12-24 00:58

    var settingsSection = config.GetSection["someSettings:subSettings"];
    var subSettings = new List;
    
    foreach (var section in settingsSection.GetChildren())
    {
        subSettings.Add(section.Value);
    }
    

    This should give you the values you need, stored in subSettings

    Apologies for bringing up a semi-old thread. I had difficulty finding an answer as a good amount of methods are deprecated, like Get and GetValue. This should be fine if you only need a simple solution without the configuration binder. :)

提交回复
热议问题