【发布时间】: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