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

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

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

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


        
5条回答
  •  借酒劲吻你
    2020-12-24 01:00

    In my case configuration

     services.Configure>(Configuration.GetSection("ApiKeysList"));
    

    wasn't loaded because the properties were read-only and there were no default constructor

     **//not working** 
      public class ApiKey : IApiKey
        {
            public ApiKey(string key, string owner)
            {
                Key = key;
                OwnerName = owner;
            }
            public string Key { get;  }
            public string OwnerName { get;}
        } 
    

    //Working

        public class ApiKey : IApiKey
        {
            public ApiKey(){}//Added default constructor
            public ApiKey(string key, string owner)
            {
                Key = key;
                OwnerName = owner;
            }
            public string Key { get; set; }        //Added set property
            public string OwnerName { get; set; }  //Added set property
        } 
    

提交回复
热议问题