繁体   English   中英

如何将嵌套的 json 配置绑定到 .net 核心中的选项

[英]How to bind nested json configuration to options in .net core

在 .net 内核中,如果我们必须直接从 Json 配置中绑定配置,如下所示

{  
  "AzureAdJwtSettings": {
    "Authority": "https://login.microsoftonline.com/tenantid",
  },
  "WebSecJwtSettings": {
    "Authority": "https://example.com",
  }
}

我们可以写这样的东西

configuration.Bind("AzureAdJwtSettings", options);

我正在寻找一种方法,以便我可以以这种方式对我的 json 配置进行分组

{  
  "JwtSettings": {
    "AzureAdJwtSettings": {
      "Authority": "https://login.microsoftonline.com/tenantid"
    },
    "WebSecJwtSettings": {
      "Authority": "https://example.com"
    }
  }
}

但是当我尝试在我的代码中加载配置时,它没有正确加载。我正在使用下面的代码

configuration.Bind("JwtSettings.AzureAdJwtSettings", options);

我知道必须有办法加载嵌套属性,但找不到它工作......

根据建议的评论,格式应使用冒号 ( : ) 而不是点 ( . ) 与配置选项绑定工作的键

例如

configuration.Bind("JwtSettings:AzureAdJwtSettings", options);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM