Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 641
Vnuuk Avatar asked Oct 24 '16 09:10

Vnuuk


People also ask

Where is connection string in asp net core application?

In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings. json , an environment variable, the user secret store, or another configuration source. See the Configuration section of the ASP.NET Core documentation for more details.

How do I find my connect string?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.


1 Answers

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);

like image 137
juunas Avatar answered Oct 20 '22 13:10

juunas