How to read connection string in ASP .NET Core Razor Pages

Dawid Kubiak

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.

I have tried the following methods:

  • Using ConfigurationManager.AppSettings a Count returns 0
  • Using ConfigurationManager.ConnectionStrings[].ConnectionString - 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
  • Trying to use WebConfigurationManager can't find how to add it. I've tried System.Web.Configuration, NuGet packages, references.
  • Using IConfiguration and dependency injection - 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)
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

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.

appsettings.json

{
  "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": "*"
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <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"/>
    <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"/>
  </connectionStrings>
</configuration>
Củ Su Hào

You can create an AppSettings class like this, by copying your appsettings.json to QuickType:

public class AppSettings
{
    public static ConnectionStrings ConnectionStrings { get; set; }
    public static Logging Logging { get; set; }
    public static string AllowedHosts { get; set; }
}

public class ConnectionStrings
{
    public string CustomerDb { get; set; }
    public string SupplierDb { get; set; }
}

public class Logging
{
    public LogLevel LogLevel { get; set; }
}

public class LogLevel
{
    public string Default { get; set; }
    public string Microsoft { get; set; }
    public string MicrosoftHostingLifetime { get; set; }
}

Then in Startup:

public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        Configuration.Get<AppSettings>();
        services.AddControllersWithViews();
    }

I use static for easy access instead of injecting AppSettings through DI.

Or if you don't want to put them all in a class, check out the Options pattern from Microsoft.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to redirect on ASP.Net Core Razor Pages

How to deny anonymous users in asp.net core razor pages?

How to extend an ASP.NET Core MVC project by Razor Pages?

How to implement pagination in asp.net core razor pages

How to create shared form in ASP.NET CORE Razor Pages?

How to display another view in ASP.NET Core Razor Pages

How to read connection string in .NET Core?

Authorization in ASP .NET Core Razor pages

How to read connection string inside .NET Standard Class library project from ASP.NET Core

I cannot retrieve connection string in DbContext class in .NET Core 2.2 Razor Pages

How to integrate Razor Components into existing Razor Pages Asp.net Core project with multiple routes

Setting Kendo UI Grid DataSource Read property with Handler in ASP.NET Core MVC with Razor Pages

How to read connection string from appsettings.json in a DBContext file in Asp.Net Core?

Rename Pages/Shared directory in ASP.NET Core Razor Pages

ASP.NET Core Razor Pages - How to bind an asp-page-handler to a checkbox without Javascript

ASP.NET Core Razor pages vs Full MVC Core

How do you add AWS ASP.NET Core Logging to an ASP.NET Core 2.0 Razor Pages app?

How do I access the HostingEnvironment within a layout in Razor Pages ASP.NET Core?

C# ASP.NET Core Razor pages: How to access route constraint within markup

How to avoid rendering Asp.net Core razor pages when redirecting?

How to enable external authentication in an ASP.NET Core Razor Pages project?

How to call funtions defined in controller in asp.net core razor pages project

ASP.NET Core 3.1 Razor Pages: How to automatically redirect to Login page from Index page?

How to use sections properly in a layout hierarchy in ASP.NET Core MVC Razor pages?

ASP.NET Core 2.1 Identity: How to remove the Default UI razor pages?

How do I post the row id in a custom built ASP.NET Core Razor Pages editable Grid?

How do I display a confirmation message with trying to delete a record in ASP.NET Core using Razor Pages

How do I post an IFormFile in ASP.Net Core 2.0 Razor Pages?

How to uploade images to disk in asp.net core 2 , razor pages