[英]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 this要
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.