c# - 如何从 .NET Core 中的 appsettings.json 读取节值

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

我正在使用 .NET Core 5.0。我在 appsettings.json 文件中有以下部分。

"Policies": [
        {
            "name": "ApproverPolicy",
            "description": "ApproverPolicy",
            "rules": [
                {
                    "name": "abc@gmail.com",
                    "description": "abc@gmail.com"
                },
                {
                    "name": "def@gmail.com",
                    "description": "def@gmail.com"
                }
            ]
        },
        {
            "name": "RegionPolicy",
            "description": "RegionPolicy",
            "rules": [
                {
                    "name": "East US",
                    "description": "East US"
                },
                {
                    "name": "North Europe",
                    "description": "North Europe"
                }
            ]
        }
    ]

我定义了以下 C# 类。

public class Policy : PolicyDefinition
{
    public IList<Rule> Rules { get; set; }
}
        
public class PolicyDefinition 
{
    public string Name { get; set; }
    public string Description { get; set; }
}
    
public class Rule
{
    public string Name { get; set; }
    public string Description { get; set; }
    public RuleStatus Status { get; set; }
    public DateTime UpdatedAt { get; set; }
    public string UpdatedBy { get; set; }
}
    
public enum RuleStatus
{
    Undefined = 0,
    Blocked = 1,
    Approved = 2,
    Rejected = 4,
    Unknown = 8
}

我正在使用以下代码从 appsettings.json 文件中读取策略部分。名称和描述属性按预期填充,但“规则”属性为 NULL。正确填充规则属性需要进行哪些更改? 非常感谢您的帮助。

_configuration.GetSection("Policies")
              .GetChildren()
              .ToList()
              .Select(x => new Policy
               {
                   Name = x.GetValue<string>("name"),
                   Description = x.GetValue<string>("description"),
                   Rules = x.GetValue<IList<Rule>>("rules")
                }).ToList();

以下代码在这种情况下有效。感谢大家的帮助。

_configuration.GetSection("Policies").Get<List<Policy>>().ToList();

最佳答案

声明一个与您的 appsettings 结构匹配的类更容易。然后您可以在启动时设置它们并使用 IOptions 将其注入(inject)到类中。

用于启动

 var appSettingsSection = Configuration.GetSection("Policies");
 services.Configure<List<Policy>>(appSettingsSection);
 var appSettings = appSettingsSection.Get<List<Policy>>();

enter image description here

然后当你注入(inject)

public SomeClass(IOptions<List<Policy>> options){
     var policies = options.Value;
}

enter image description here

关于c# - 如何从 .NET Core 中的 appsettings.json 读取节值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65933664/

相关文章:

c# - 适用于通用 Windows 平台 (UWP) 应用程序的 Prism

c# - SignalR 无法启动连接

jquery - 如何在 Razor Pages 中序列化表单并发送 Ajax 请求

Azure应用程序配置: Double underscore not equivalent to colon when trying to denote nesting

.net-core - Dotnet publish with .NET core 2.0 和 .NET Framework 项目

c# - 为什么我不能将资源用作带有 DataAnnotations 的 ErrorMessage?

c# - 在 Rider 中重新编译 ASP.NET Core 应用程序而无需重复重新运行它

C# 区分用户形式 close 和 o.s.形式关闭

asp.net-core - 如何以编程方式重定向到 Razor 代码中的调用者?

unit-testing - 如何在不构建项目的情况下在命令行中运行xUnit测试DLL