c# - 如何从 .net core 中的 appsettings.json 中提取列表

标签 c# asp.net-core .net-core

我有一个 appsettings.json 文件,如下所示:

{
    "someSetting": {
        "subSettings": [
            "one",
            "two",
            "three"
         ]
    }
}

当我构建我的配置根目录并执行类似 config["someSetting:subSettings"] 的操作时,它返回 null 并且可用的实际设置如下所示:

配置["someSettings:subSettings:0"]

是否有更好的方法以列表的形式检索 someSettings:subSettings 的内容?

最佳答案

假设您的 appsettings.json 如下所示:

{
  "foo": {
    "bar": [
      "1",
      "2",
      "3"
    ]
  }
}

您可以像这样提取列表项:

Configuration.GetSection("foo:bar").Get<List<string>>()

关于c# - 如何从 .net core 中的 appsettings.json 中提取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169701/

相关文章:

asp.net-core - 如何在 ASP.NET Core 3.1 中使用简单注入(inject)器进行依赖交换以进行集成测试

c# - Microsoft.Web.Administration.Site 在调用方法 Stop() 时抛出 NotImplemenetedException

c# - 通过 Session 变量初始化一个值

c# - 添加表代码先迁移 Entity Framework

.net-core - 如何替换 .NET Standard 的 System.ComponentModel.DataAnnotations 类?

asp.net - ASP Core、Identity Server 4 和 Angular 2 中的外部提供商登录

asp.net - 在.NET Core中更新身份登录页面的默认前端设计

c# - 如何从 POCO 对象集合中检索属性的最高/最后值?

c# - 通过作为字符串传递的名称引用 c# 对象字段

docker - .Net Core 不能使用支持 Docker 的 Add-migrations