简体   繁体   English

Configuration.GetSection 从 appsetting.json 获取值但 Configuration.GetSection.Bind 总是返回 null

[英]Configuration.GetSection gets value from appsetting.json but Configuration.GetSection.Bind always returns null

I have below code where I am trying to bind my appsettings.json with my variable and my variable is of class type whose model has been defined appropriately as per JSON schema.我有下面的代码,我试图将我的 appsettings.json 与我的变量绑定,我的变量属于类类型,其模型已根据 JSON 模式进行了适当的定义。

During Debug, In quick watch I am able to see the value of my appsettings.json in config.GetSection("TableStorageRule") but for config.GetSection("TableStorageRule").Bind(tableStorageOutput) its null .在调试期间,在快速观察中,我能够在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;

I would like to know what's wrong I am doing?我想知道我在做什么错?

Here is my model class definition这是我的模型类定义

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; }
}

Here is my Json Schema>这是我的 Json 架构>

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

The problem is in your Json.问题出在您的 Json 中。 TableStoreSettings needs to be renamed to TableSettings to match the class, and your TableStorageRule is not an array of rules. TableStoreSettings需要重命名为TableSettings以匹配该类,并且您的TableStorageRule不是规则数组。

{
  "TableStorageRule": {
    "Name": "filterRule1",
    "DataFilter": {
      "DataSetType": "Settings1"

    },
    "TableSettings": {
      "AzureTable": {
        "Account": "account1",
        "Table": "table1",
        "Key": "key1"
      },
      "SchemaBaseUri": "https://test.web.core.windows.net/"
    }
  }
  
}

If you are planning on having an array of rules, I would recommend putting another Top Level class.如果您打算制定一系列规则,我建议您再开设一个Top Level课程。

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

Then your Json would look like this然后你的 Json 看起来像这样

{
  "TableStorageRule": {
    "Rules": [
      
      {
        "Name": "filterRule1",
        "DataFilter":
        {
          "DataSetType": "Settings1"

        },
        "TableSettings":
        {
          "AzureTable": {
            "Account": "account1",
            "Table": "table1",
            "Key": "key1"
          },
          "SchemaBaseUri": "https://test.web.core.windows.net/"
        }

      }
    ]
  }

}

To Bind you would use thisBind您将使用此

        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;

暂无
暂无

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

相关问题 Configuration.GetSection返回null值 - Configuration.GetSection returns null value Configuration.GetSection 总是返回 Value 属性 null - Configuration.GetSection always returns Value property null configuration.getValue 或 configuration.getsection 总是返回 null - configuration.getValue or configuration.getsection always returns null Configuration.GetSection(“的connectionStringName”)。获取 <?> 总是为空 - Configuration.GetSection(“ConnectionStringName”).Get<?> always null 配置 GetSection 返回对象部分的空值 - Configuration GetSection returns null value for object sections Configuration.GetSection()轻松获取原始字符串值,但不获取复杂值 - Configuration.GetSection() easily gets primitive string values but not complex values 使用手动添加的 settings.json 文件到 dotnet 核心控制台应用程序时,Configuration.GetSection(“SectionName”) 始终为 null - Configuration.GetSection(“SectionName”) is always null when using manually added settings.json file to dotnet core console app Configuration.GetSection 返回文件中不存在的 ConfigurationSection - Configuration.GetSection returns a ConfigurationSection that doesn't exists in the file ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? 为什么 static Configuration.GetSection() 不可用? - Why is static Configuration.GetSection() not available?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM