Getting ConnectionStrings config settings from appsettings.json

Sam

In my repository class, I have my Config object and looks like my connection string is under:

Config > Providers > Microsoft.Configuration.Json.JsonConfigurationProvider > Data > ConnectionStrings.myConnectionString

This is what my appsettings.json looks like:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "myConnectionString": details..."
  }
}

I'm trying to read myConnectionString as follows which is not working:

var cs = _config.GetSection("ConnectionStrings.myConnectionString").value;

What am I doing wrong?

UPDATE: For some reason, I'm not seeing GetValue() method. I'm using ASP.NET Core 2.0.

enter image description here

smn.tino

The issue seems to lie in the string path you are passing to the GetSection() method. According to the ASP.NET Core Configuration documentation you should use "ConnectionStrings:myConnectionString" instead of "ConnectionStrings.myConnectionString".

Plus, if you wish to retrieve the value directly you may prefer to use the GetValue() method instead:

var cs = _config.GetValue("ConnectionStrings:myConnectionString", "");

If you prefer, you can also use the index notation as:

var cs = _config["ConnectionStrings:myConnectionString"];

But I honestly find the first approach more clean and elegant as the GetValue() method allows you to specify a default value in case the property is not found in the configuration.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to move EF6.1 connection string from connectionStrings to an appSettings config file section?

From Dev

MVC 3 getting values from AppSettings in web.config

From Java

Getting value from appsettings.json in .net core

From Dev

Json in Web.Config AppSettings

From Dev

How to create a JSON string with key/value pairs from web.config appsettings?

From Dev

.NET Core 3.1 loading config from appsettings.json for console application

From Java

Override array settings in appsettings.json with those in appsettings.Production.json

From Dev

Read file attribute from appsettings from web.config

From Dev

Getting settings object from the buildscript

From Dev

Access connection string from (ARM Template) WebSite.json OR override Web.config with ARM Template Settings

From Dev

web.config connectionStrings on production server

From Dev

How to get keys and values from Appsettings of Config file in Linq

From Dev

Access appSettings in web.config from Thread in Class Library project

From Dev

Using appSettings key from web.config in NavigateUrl property

From Dev

Bind list of string from appsettings.json

From Dev

Unable to read details from appsettings.json

From Dev

Configuration classes from appsettings.json

From Dev

Retrieving data from appsettings.json

From Dev

WCF channelfactory with settings from the config file?

From Java

Reading settings from app.config or web.config in .NET

From Dev

Backbone and best practice getting config JSON

From Dev

getting setting from web.config in sitecore

From Dev

getting send grid credentials from web config

From Dev

Getting a parameter from a config file in FreeBSD

From Dev

Getting variables From a config file to the browser

From Dev

Getting JSON from PHP

From Dev

Getting JSON from Form

From Dev

Getting JSON from Form

From Dev

Getting JSON from PHP

Related Related

  1. 1

    How to move EF6.1 connection string from connectionStrings to an appSettings config file section?

  2. 2

    MVC 3 getting values from AppSettings in web.config

  3. 3

    Getting value from appsettings.json in .net core

  4. 4

    Json in Web.Config AppSettings

  5. 5

    How to create a JSON string with key/value pairs from web.config appsettings?

  6. 6

    .NET Core 3.1 loading config from appsettings.json for console application

  7. 7

    Override array settings in appsettings.json with those in appsettings.Production.json

  8. 8

    Read file attribute from appsettings from web.config

  9. 9

    Getting settings object from the buildscript

  10. 10

    Access connection string from (ARM Template) WebSite.json OR override Web.config with ARM Template Settings

  11. 11

    web.config connectionStrings on production server

  12. 12

    How to get keys and values from Appsettings of Config file in Linq

  13. 13

    Access appSettings in web.config from Thread in Class Library project

  14. 14

    Using appSettings key from web.config in NavigateUrl property

  15. 15

    Bind list of string from appsettings.json

  16. 16

    Unable to read details from appsettings.json

  17. 17

    Configuration classes from appsettings.json

  18. 18

    Retrieving data from appsettings.json

  19. 19

    WCF channelfactory with settings from the config file?

  20. 20

    Reading settings from app.config or web.config in .NET

  21. 21

    Backbone and best practice getting config JSON

  22. 22

    getting setting from web.config in sitecore

  23. 23

    getting send grid credentials from web config

  24. 24

    Getting a parameter from a config file in FreeBSD

  25. 25

    Getting variables From a config file to the browser

  26. 26

    Getting JSON from PHP

  27. 27

    Getting JSON from Form

  28. 28

    Getting JSON from Form

  29. 29

    Getting JSON from PHP

HotTag

Archive