First of all, the
"Data": {
"ConnectionStrings": {
"DefaultConnection": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"},
}
Is slightly different from the structure you get when you add a “Asp.NET Configuration File” in Visual Studio. When you do that you get
"ConnectionStrings": {
"DefaultConnection": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"},
without the “Data” JavaScript Object. So that’s why the extension method isn’t working. It expects this structure. You can use this structure (the one with “Data”) anyway and get your connection string like so:
var connectionString = Configuration["Data:ConnectionStrings:DefaultConnection"];
Notice that you are navigating through the JavaScript object tree using :
instead of .
. That’s due to some cross-platform issues with using the .
.
If you edit out the “Data”:{} you can do this :
var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
Now the extension method will work. Underneath the Microsoft extensions it is the same thing as the code above.
var config2 = Configuration.GetConnectionString("DefaultConnection");