Hi,
I am new to web api core,I have created one web api core with ado.net application.
I have declared a conenction string in appseeting.json,
I am trying new access in the controller but i am getting null value?
can any one help me how to access conenction string in controller from appsetting.sjon
step1
appsettins.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLOCALDB;Database=resource;Trusted_Connection=True;"
}
}
step 2
SqlConnection con1 = new SqlConnection(configuration.GetConnectionString("DefaultConnection"));

Rijwan AnsariRank #82 · 24.2K reputationPosted Aug 10, 2023, 9:05 AM EDT
Hi
Try this way.
string connStr = configuration.GetConnectionString("DefaultConnection"); SqlConnection con1 = new SqlConnection(connStr);Tahir AnsariRank #366 · 6.8K reputationPosted Aug 10, 2023, 10:09 AM EDT
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System.Data.SqlClient; namespace Demo.API.Controllers { [ApiController] [Route("api/[controller]")] public class HomeController : ControllerBase { private readonly IConfiguration _configuration; public HomeController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public IActionResult Index() { string connectionString = _configuration.GetConnectionString("DefaultConnection"); // Use the connection string as needed... using (SqlConnection connection = new SqlConnection(connectionString)) { // Your SQL operations here... // Example: connection.Open();, execute queries, etc. } return Ok("Connection successful."); } } }Saravanan GanesanRank #153 · 15.1K reputationPosted Aug 16, 2023, 6:14 AM EDT
In ASP.NET Core, inject
IConfigurationinto your controller's constructor. Use_configuration.GetConnectionString("DefaultConnection")to access the connection string fromappsettings.json. This allows you to create aSqlConnectioninstance for database operations within your controller methods. Ensure namespaces, class names, and methods match your codebase.Mohammad HussainRank #513 · 4.8K reputationPosted Aug 24, 2023, 12:38 AM EDT
It looks like you're on the right track, but there's a missing piece in your code that's causing the
nullvalue. You need to inject theIConfigurationinterface into your controller so that you can access the connection string fromappsettings.json.Amit MohantyRank #112 · 20K reputationPosted Aug 10, 2023, 9:42 AM EDT
Try this
public class YourController : ControllerBase { private readonly IConfiguration _configuration; public YourController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public IActionResult Get() { string dbConnection = _configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value; SqlConnection sqlConnection = new SqlConnection(dbConnection); return NotFound(); } }Jaimin ShethiyaRank #177 · 13.7K reputationPosted Aug 22, 2023, 12:25 AM EDT
Hello Hari,
Can you please check on your startup.cs file have you registered the configuration, if not then can you please add and try after that.
services.AddSinglton(configuration);
then after you can inject the IConfiguration into your API controller
private readonly IConfiguration _configuration;
public ValuesController(IConfiguration configuration)
{
_configration = configuration;
}
[HttpGet]
public IActionResult Get()
{
string connectionString = _configuration.GetConnectionString("DefaultConnection");
.......
}
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 24, 2023, 1:35 AM EDT
Please try adding
IConfigurationinterface into your controller and try the samePrasad RaveendranRank #320 · 7.8K reputationPosted Aug 25, 2023, 7:11 PM EDT
Since you are a beginner. its a good start with understanding the interface IConfiguration. However, I would recommend to explore the Options pattern - there are multiple ways to ready the AppSettings.json.
https://learn.microsoft.com/en-us/dotnet/core/extensions/options