Store and read connection string in appsettings.json

There are different ways to store and read connection strings in appsettings.json. Learn some easy ways to work with connection strings in .NET Core.
Written by: Mark Lurade

In .NET Core and ASP.NET Core applications the application settings are stored in a JSON file named appsettings.json. This is the default configuration for .NET Core projects. INI and XML files is also supported but JSON is the default.

Method 1: Using the standard location

To define the connection strings in appsettings.json it is important to specify it in the right section of the JSON structure.

{
  "ConnectionStrings": {
    "myDb1": "Server=myServer;Database=myDb1;Trusted_Connection=True;",
    "myDb2": "Server=myServer;Database=myDb2;Trusted_Connection=True;"
  }
}

Now we can read it in our code by calling the GetConnectionString method in the Microsoft.Extensions.Configuration namespace.

string myDb1ConnectionString = _configuration.GetConnectionString("myDb1");
string myDb2ConnectionString = _configuration.GetConnectionString("myDb2");

To be able to call the method you must import the Microsoft.Extensions.Configuration namespace like this:

using Microsoft.Extensions.Configuration;

Method 2: Using a custom appsettings structure with sections

If you want to structure your settings in a way more logical to your app it is possible to locate your connection strings whereever you want and still be able to access them in code.

{
  "Modules": {
    "Logging": {
      "logDb": "Server=myServer;Database=myDb1;Trusted_Connection=True;",
    },
    "Tenants": {
      "tenantsDb": "Server=myServer;Database=myDb1;Trusted_Connection=True;",
    }
  } 
}

In this scenario we do not use the GetConnectionString method in the Microsoft.Extensions.Configuration namespace. Instead we use the GetValue method and use colon ":" to represent the hierarchy.

string logDbConnectionString = _configuration.GetValue<string>("Modules:Logging:logDb"); // read logDb connection string example
string tenantsDbConnectionString = _configuration.GetValue<string>("Modules:Tenants:tenantsDb"); // read tenantsDb connection string example

Accessing the Configuration object

For the above methods to work we need access to the configuration object ("_configuration" in the above samples) which is an IConfiguration. This object is registered in the .NET Core IoC container so we can easily get access to it through dependency injection.

Access configuration in Startup.cs

public class Startup
{
    private readonly IConfiguration _configuration;

    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }
}

Access configuration in ASP.Net MVC controller

public class HomeController : Controller
{
    private readonly IConfiguration _configuration;

    public HomeController(IConfiguration configuration)
    {
        _configuration = configuration;
    }
}

About settings in .NET Core

Please note that this article only demonstrates a few easy ways to work with connection strings specifically. There is many other ways to work with configuration settings in .NET Core. For instance:

  • The IOptions interface
  • The IServiceCollection Configure method
  • The IConfigurationSection Bind method

Summary

Storing connection strings in appsettings.json makes it easy to change settings in production without touching the codebase or re-compiling. Once you learn about configuration in .NET Core and what methods to use it is quite easy to work with.

Connect to