简体   繁体   English

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

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

In .net core if we have to bind the configuration directly from Json configuration as shown below在 .net 内核中,如果我们必须直接从 Json 配置中绑定配置,如下所示

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

we can write something like this我们可以写这样的东西

configuration.Bind("AzureAdJwtSettings", options);

I am looking to find a way so that I can group my json configuration in such a way我正在寻找一种方法,以便我可以以这种方式对我的 json 配置进行分组

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

But when I try to load the configuration in my code it didn't load properly.. I am using the below code但是当我尝试在我的代码中加载配置时,它没有正确加载。我正在使用下面的代码

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

I know there must be way to load nested properties but couldn't find it working...我知道必须有办法加载嵌套属性,但找不到它工作......

As per suggested comments, the format should use colon ( : ) instead of dot ( . ) with keys for configuration option binding to work根据建议的评论,格式应使用冒号 ( : ) 而不是点 ( . ) 与配置选项绑定工作的键

For example例如

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

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

相关问题 .NET Core 使用配置绑定到带数组的选项 - .NET Core use Configuration to bind to Options with Array 如何将配置绑定到JSON对象.Net Core数组 - How to bind configuration to array of JSON Objects .Net Core .net核心中的配置选项 - Configuration Options in .net core 如何在 .NET Core 3.1 中绑定新选项 object? - How do you bind a new options object in .NET Core 3.1? 无法使用 Asp.Net Core 3 中的命名选项读取/绑定具有强类型 class 的配置设置 - Unable to read/bind configuration settings with strongly-typed class using Named Options in Asp.Net Core 3 如何从方法主体中提取配置选项并在 Dot Net Core 3.1 中使用配置 class - How to pull configuration options out of method body and use configuration class in Dot Net Core 3.1 在ASP.NET Core中访问配置选项:选项模式 - Accessing configuration options in ASP.NET Core: options pattern 如何使用 .net 核心 3.1 中的 ConfigurationBulder 绑定到配置 class - How can I bind to a configuration class using ConfigurationBulder in .net core 3.1 如何在.net Core应用程序中使用IConfiguration绑定多级配置对象? - How do I bind a multi level configuration object using IConfiguration in a .net Core application? ASP.NET Core 中的选项模式 - 如何将子选项作为 JSON 字符串返回(非强类型) - Options Pattern in ASP.NET Core - how to return sub-options as a JSON string (not strongly typed)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM