【问题标题】:Configuration.GetSection gets value from appsetting.json but Configuration.GetSection.Bind always returns nullConfiguration.GetSection 从 appsetting.json 获取值,但 Configuration.GetSection.Bind 始终返回 null
【发布时间】:2020-11-02 15:06:22
【问题描述】:

我有下面的代码,我试图将我的 appsettings.json 与我的变量绑定,并且我的变量属于类类型,其模型已根据 JSON 模式适当定义。

在调试期间,在快速观察中,我可以在 config.GetSection("TableStorageRule") 中看到我的 appsettings.json 的值,但对于 config.GetSection("TableStorageRule").Bind(tableStorageOutput) 它的 null

var builder = new ConfigurationBuilder()
        .SetBasePath(Path.Combine(Root))
        .AddJsonFile("appsettings.json", optional: false);

var config = builder.Build();

var tableStorageOutput = new TableStorageRule();            
config.GetSection("TableStorageRule").Bind(tableStorageOutput);
var nameOfFilter = tableStorageOutput.Name;

我想知道我做错了什么?

这是我的模型类定义

public class TableStoreSettings
{
    public class AzureTableSettings
    {
        public string Account { get; set; }
        public string Key { get; set; }
        public string Table { get; set; }
    }

    public AzureTableSettings AzureTable { get; set; }

    public Uri SchemaBaseUri { get; set; }
}

public class TableStorageRule
{
    public string Name { get; set; }
    public TwisterDataFilter DataFilter { get; set; }
    public TableStoreSettings TableSettings { get; set; }
}

这是我的 Json 架构>

{  "TableStorageRule": [
  {
    "Name": "filterRule1",
    "DataFilter": {
      "DataSetType": "Settings1"
    
    },
    "TableStoreSettings": {
      "AzureTable": {
        "Account": "account1",
        "Table": "table1",
        "Key": "key1"
      },
      "SchemaBaseUri": "https://test.web.core.windows.net/"
    }
  } 
]}

【问题讨论】:

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


    【解决方案1】:

    问题出在您的 Json 中。 TableStoreSettings 需要重命名为TableSettings 以匹配类,并且您的TableStorageRule 不是规则数组。

    {
      "TableStorageRule": {
        "Name": "filterRule1",
        "DataFilter": {
          "DataSetType": "Settings1"
    
        },
        "TableSettings": {
          "AzureTable": {
            "Account": "account1",
            "Table": "table1",
            "Key": "key1"
          },
          "SchemaBaseUri": "https://test.web.core.windows.net/"
        }
      }
      
    }
    

    如果您打算制定一系列规则,我建议您再添加一个 Top Level 类。

        public class TableStorageRules
        {
            public List<TableStorageRule> Rules { get; set; }
        }
    

    那么你的 Json 会是这个样子

    {
      "TableStorageRule": {
        "Rules": [
          
          {
            "Name": "filterRule1",
            "DataFilter":
            {
              "DataSetType": "Settings1"
    
            },
            "TableSettings":
            {
              "AzureTable": {
                "Account": "account1",
                "Table": "table1",
                "Key": "key1"
              },
              "SchemaBaseUri": "https://test.web.core.windows.net/"
            }
    
          }
        ]
      }
    
    }
    

    Bind你会用这个

            var builder = new ConfigurationBuilder()
                            .SetBasePath(Path.Combine(Root))
                            .AddJsonFile("appsettings.json", optional: false);
    
            var config = builder.Build();
    
            var tableStorageOutput = new TableStorageRules();
            config.GetSection("TableStorageRule").Bind(tableStorageOutput);
            var nameOfFilter = tableStorageOutput.Rules[0].Name;
    

    【讨论】:

    • 按照您的步骤,我得到 Null 值。我确实需要你描述的一系列规则。
    • 什么是空值?
    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 2018-11-13
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 2015-08-15
    相关资源
    最近更新 更多