如何在.net core中把嵌套的json配置绑定到选项上?

问题描述 投票:0回答:1

在.net core中,如果我们要从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);

我知道一定有办法加载嵌套的属性,但找不到工作......

c# asp.net asp.net-core
1个回答
1
投票

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

比如说

configuration.Bind("JwtSettings:AzureAdJwtSettings", options);
© www.soinside.com 2019 - 2024. All rights reserved.