![](/img/trans.png)
[英]How can I get my .NET Core 3 single file app to find the appsettings.json file?
[英]How can I convert my appsettings.json section to an object?
我有一個appsettings.json ,看起來像這樣:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"SearchExclusions": {
"Classifications": {
"title": "My First Title",
"entries": [ "ALPHA", "BETA" ]
},
"SubjectContains": {
"title": "My Second Title",
"entries": [ "GAMMA", "DELTA", "EPSILON", "ZETA", "ETA", "THETA", "IOTA", "KAPPA" ]
}
}
}
我的.RAZOR組件如下所示:
@inject IConfiguration Configuration
<h1> My Component </h1>
@code {
protected override void OnInitialized()
{
// This does not work.
var searchExclusionsObject = Configuration.GetSection("SearchExclusions").Get<SearchExclusions>();
// This also does not work.
var searchExclusionsObject = Configuration.Get<SearchExclusions>();
}
private class SearchExclusions
{
private Classifications classifications { get; set; }
private SubjectContains subjectContains { get; set; }
}
private class Classifications
{
private String title { get; set; }
private String[] entries { get; set; }
}
private class SubjectContains
{
private String title { get; set; }
private String[] entries { get; set; }
}
}
SearchExclusions 和其他子類上的所有屬性都是私有的,它們需要是公共的。
當然,只是需要將屬性的可訪問性從 Private 更改為 Public
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.