Store String Array In appSettings?

前端 未结 5 1741
借酒劲吻你
借酒劲吻你 2020-11-29 07:57

I\'d like to store a one dimensional string array as an entry in my appSettings. I can\'t simply separate elements with , or | because

5条回答
  •  情话喂你
    2020-11-29 08:53

    ASP.Net Core supports it binding a list of strings or objects.

    For strings as mentioned, it is possible to retrieve it through AsEnumerable().

    Or a list of objects via Get(). The sample is below.

    appsettings.json

    {
    ...
      "my_section": {
        "objs": [
          {
            "id": "2",
            "name": "Object 1"
          },
            "id": "2",
            "name": "Object 2"
          }
        ]
      }
    ...
    }
    

    Class to represent the object

    public class MyObject
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    

    Code to retrieve from appsettings.json

    Configuration.GetSection("my_section:objs").Get>();
    

提交回复
热议问题