The second answer of this question: ASP.NET Core 6 how to access Configuration during startup mentioned that we can access configuration in this way:
A nice feature worth considering it to create a class that represents your settings and then bind the configuration to an instance of that class type. For example, let's assume you create a new class called MyAppSettings with the same structure as your appSettings.json, you can do the following:
var myAppSettings = builder.Configuration.Get<MyAppSettings>();
string logLevel = myAppSettings.Logging.LogLevel.Default;
I am confused about it and I want to know how to do that and what are the benefits of this.
What's more, the best method to access configuration in .NET 6?
Explain my question in more detail. The appSetting.json file in my project has only one layer like this:
{
"name": "jack",
...1000+,
"age": "100"
}
So I cannot configure it using builder.congiguration.GetSection():
builder.Services.Configure<XXX>(builder.Configuration.GetSection("XXX"));
How to configure it at once without changing the structure of appSettings.json?
thank you again~