Robert
Robert

Reputation: 4406

How do you get a connection string from the appsettings.json in .netCore 1.1?

I am following this post:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration

I am not seeing they are accessing the properties here. When I do something like this:

public MyRepository(IConfiguration configuration)
{
    _connectionString = $"{configuration["MyConnectionString"]}";
}

All I get back are nulls or empty strings

appconfig.json

 "ConnectionString": {
    "MyConnectionString": "CorrectConnectionString"

}

If I inspect the configuration during runtime. I can see that the connectionstring is stored in it, I just can't figure out how to access it.

What am I missing?

Upvotes: 1

Views: 1314

Answers (1)

erikbozic
erikbozic

Reputation: 1665

Since the MyConnectionString property is defined inside ConnectionString object you need to access it like:

public MyRepository(IConfiguration configuration)
{
    _connectionString = $"{configuration["ConnectionString:MyConnectionString"]}";
}

Upvotes: 1

Related Questions