我想从静态类中的 appsettings.json 文件中读取 url。我尝试过类似的东西
private static string Url => ConfigurationManager.GetSection("Urls/SampleUrl").ToString();
Run Code Online (Sandbox Code Playgroud)
但每当我尝试调用GetSection()方法时,我都会得到空值。
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"cs1": "some string",
"cs2": "other string"
},
"Urls": {
"SampleUrl": "google.com"
},
"AllowedHosts": "*"
Run Code Online (Sandbox Code Playgroud)
我只是想从应用程序设置中读取一些数据。根据文档,我不应该以某种方式注册我的标准 appsettings.json 文件,因为Host.CreateDefaultBuilder(args)在 Program 类中默认为我注册。