title: How to read connection string in ASP .NET Core Razor Pages
body: <p>I can't wrap my head around how to retrieve connection strings in ASP .NET Core. I've spent two days trying all the different things and can't get it to work.</p>
  
  <p>I have tried the following methods:</p>
  
  <ul>
  <li>Using <strong>ConfigurationManager.AppSettings</strong> a Count returns 0</li>
  <li>Using <strong>ConfigurationManager.ConnectionStrings[].ConnectionString</strong> - the only connection string that exists is to .\SQLEXPRESS (have to use index rather than string in the ConnectionStrings[]), specifying a name of the connection results in object reference not set error</li>
  <li>Trying to use <strong>WebConfigurationManager</strong> can't find how to add it. I've tried System.Web.Configuration, NuGet packages, references.</li>
  <li>Using <strong>IConfiguration and dependency injection</strong> - I've got no idea how to read the connection string (how to reference the controller in the model and read the value of "xxx" connection string) </li>
  </ul>
  
  <pre><code>ConfigurationManager.ConnectionStrings["SupplierDB"].ConnectionString
  // NullReferenceException: Object reference not set to an instance of an object.
  
  ConfigurationManager.ConnectionStrings[0].ConnectionString
  // data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
  
  ConfigurationManager.AppSettings.Count.ToString();
  // Returns 0
  </code></pre>
  
  <p>I've got same connection strings specified in both appsettings.json and web.config. Can someone please point me in the right direction as I'm currently completely lost as nothing works.</p>
  
  <p><strong>appsettings.json</strong></p>
  
  <pre><code>{
    "ConnectionStrings": {
      "CustomerDB": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Customer_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
      "SupplierDB": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Supplier_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    },
    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "AllowedHosts": "*"
  }
  </code></pre>
  
  <p><strong>web.config</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="CustomerDB" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Customer_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/&gt;
      &lt;add name="SupplierDB" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Supplier_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>