Vnuuk Vnuuk - 1 month ago 21
ASP.NET (C#) Question

Get connection string in .NET Core application

I'm trying to get connection string dynamically from appsettings.json file. I see that I can do that via Configuration property of startup class. I've marked Configuration field as static field and access it across the app.

I'm wondering if there is a better way to get connection string value from .NET Core app.

Answer

You can check my blog article on ASP.NET Core Configuration here.

In it I also go through Dependency Injection of configuration options.

Quote:

There are a couple ways to get settings. One way would be to use the Configuration object in Startup.cs.

You can make the configuration available in your app globally through Dependency Injection by doing this in ConfigureServices in Startup.cs:

services.AddSingleton(Configuration);

Comments