Hi @Mitch McConnell ,
Reading Connection String from AppSettings.json file using IConfiguration interface.
In the below example, the IConfiguration is injected in the Controller and assigned to the private property Configuration.
Then inside the Controller, the Connection String is read from the AppSettings.json file using the GetConnectionString function.
appsettings.json
"ConnectionStrings": {
"ContosoConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Controller:
public class TestController : Controller
{
private IConfiguration Configuration;
public TestController(IConfiguration _configuration)
{
Configuration = _configuration;
}
public IActionResult Index()
{
string connString = this.Configuration.GetConnectionString("ContosoConnection");
return View();
}
}
Result:
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
ChaoDeng