从appsettings.json中的小节中获取值

时间:2019-07-09 19:23:22

标签: asp.net-core

说我的appsettings.json文件中有一个类似的部分。

"FileSettings": {
    "File1": {
      "Schedule": "1030"
    },
    "File2": {
      "Schedule": "1130"
    }
  }

我想要一个具有这样的值的匿名对象的列表,但是schedule为null。

  {File1, 1030}
  {File2,1130}

  var fileList = (from a in fileSection
                select new {
                    FileName = a.Key,
                    Schedule = fileSection.Where(b => b.Key == a.Key)
                .FirstOrDefault().GetSection(a.Key + ":" + "Schedule").Value
                }).ToList();

1 个答案:

答案 0 :(得分:1)

您可以像这样获得文件设置的children元素。

var fileSection = Configuration.GetSection("FileSettings").GetChildren();

var fileList = (from a in fileSection
                select new
                {
                    FileName = a.Key,
                    Schedule = fileSection.Where(b => b.Key == a.Key)
                .FirstOrDefault().GetSection("Schedule").Value
                }).ToList();