簡體   English   中英

Configuration.GetSection 從 appsetting.json 獲取值但 Configuration.GetSection.Bind 總是返回 null

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

我有下面的代碼,我試圖將我的 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/"
    }
  } 
]}

問題出在您的 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM