0

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~

phuzi
  • 12,078
  • 3
  • 26
  • 50
Selmo Jack
  • 67
  • 8
  • 1
    What's the confusion? – Chetan Jul 12 '22 at 03:38
  • "Best" in terms of what? Development time? Readability? Performance? – Uwe Keim Jul 12 '22 at 07:26
  • I would say this is explained in the multiple articles in docs about configuration in .NET: [Configuration in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0), [Options pattern in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0), [Configuration in .NET](https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration) – Guru Stron Jul 12 '22 at 08:14

1 Answers1

1

You can refer to this simple demo to learn about how to

create a class that represents your settings and then bind the configuration to an instance of that class type

The values in appsetting.json:

"UserCred": {
      "Username":"Jack",
      "Password":"123"
},

Create a model class:

public class User
{
    public string Username { get; set; }
    public string Password { get; set; }
}

Configure in your programs.cs:

builder.Services.Configure<User>(builder.Configuration.GetSection("UserCred"));

In the controller, use DI to inject it, then you can get the model with value:

public class HomeController : Controller
{
    private readonly IOptions<User> _configuration;

    public HomeController(IOptions<User> configuration)
    {
        _configuration = configuration;
    }

    //.......
}

enter image description here

What's the benefit?

For me, if there are many data ​​in appsetting.json, Creating a class to map them only needs to map once, You don't need to get them one by one, which makes coding more convenient.

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
  • Hi @user13862969, I check the question you update, In My opinion, You can read the whole json file then deserialize the data to the model, But If there are some other data which you don't want to bind in appsetting.json, This method is not work. – Xinran Shen Jul 13 '22 at 08:22