Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Sep 27, 2022, 3:32 AM EDT
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("Modules:Logging:logDb"); // read logDb connection string example string tenantsDbConnectionString = _configuration.GetValue("Modules:Tenants:tenantsDb"); // read tenantsDb connection string exampleAccessing 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:
Amit MohantyRank #112 · 20K reputationPosted Sep 27, 2022, 12:56 AM EDT
Check the article
https://www.aspsnippets.com/Articles/Net-Core-Read-Connection-String-from-AppSettingsjson-file.aspx
Vishal YelveRank #178 · 13.6K reputationPosted Sep 27, 2022, 1:11 AM EDT
Hi Naresh,
In appsetting.json
If you are using .net core 3.1
In Startup.cs class inside ConfigureService
public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("EmployeeAppCon"))); }If you are using .NET 6
In Program.cs file
builder.Services.AddDbContext(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); });Sachin SinghRank #66 · 26.2K reputationPosted Sep 27, 2022, 1:17 AM EDT
There are multiple ways like
{"Logging": { "LogLevel": {"Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } } ,"AllowedHosts": "*", "MyPage": {"Title": "Page1","Secret":1234}, "CS":"your connection string" }you can read it using IConfiguration one by one or can directly bind to any Class with the same properties.
public class AnyController : Controller { protected readonly IConfiguration _configuration; public AnyController(IConfiguration configuration) { _configuration = configuration; } public IActionResult FirstWay() { var value=_configuration.GetValue("MyPage:Title"); return Content(value, "text/plain");} }public class MyPage { public string Title { get; set; } public int secret{ get; set; } }public class AnyController : BaseController { private readonly MyPage _pageConfig = new MyPage(); public UserController(IConfiguration configuration) { configuration.GetSection("MyPage").Bind(_pageConfig); //_pageConfig.Title //_pageConfig.Secret} }to just get the connection string
var cs= _configuration.GetConnectionString("myCs");Rajanikant HawaldarRank #110 · 20.2K reputationPosted Sep 27, 2022, 2:10 AM EDT
https://www.c-sharpcorner.com/article/setting-and-reading-values-from-app-settings-json-in-net-core/
https://www.c-sharpcorner.com/article/all-about-appsettings-in-asp-net-core/