Gigi
Gigi

Reputation: 29461

How can you get strongly-typed config from the root?

In ASP .NET 5, you can map appsettings to a class. See this answer for an up-to-date example.

Code I've seen that does this typically looks like:

services.Configure<AppSettings>(Configuration.GetConfigurationSection("AppSettings"));

So it's expected that you have a node with that name in your appsettings.json (example taken from this article):

{
    "AppSettings" : {
        "SiteTitle" :  "My Web Site"
    },
    "Data": {
        "DefaultConnection": {
            "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;"
        }
    }
}

Is it possible to get the whole file (from the root) rather than having to declare a node such as AppSettings?

Upvotes: 0

Views: 209

Answers (1)

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28425

Yes, you can. Use the ConfigurationBinder

 var configurationBuilder = new ConfigurationBuilder();
 // Add the providers that you want here

 var config = configurationBuilder.Build();

 var options = new StrongTypeObject();
 config.Bind(options);

More examples: https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationBinderTests.cs

Upvotes: 1

Related Questions