In this article I will explain with an example, how to set SQL Server Connection String in AppSettings.json in .Net Core and ASP.Net Core MVC.
SQL Server Database has two types of Connection Strings i.e. Windows Authentication and SQL Server Authentication.
 
 
What is IConfiguration
The IConfiguration is an interface for .Net Core 2.0.
The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller.
The IConfiguration interface is used to read Settings and Connection Strings from AppSettings.json file.
 
 
Namespaces
You will need to import the following namespace.
using Microsoft.Extensions.Configuration;
 
 
Adding the AppSettings.json file
In order to add AppSettings.json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button.
.Net Core: Set SQL Server Connection String in AppSettings.json
 
Once the File is created, it will have a DefaultConnection, remove it and add two Connection Strings entries, one for Windows Authentication and other for SQL Server Authentication.
 
Connection String with Windows Authentication
SQL Server Connection String for Windows Authentication is defined as follows and it consists of the following properties.
Data Source – The name of the SQL Server and its Instance.
Initial Catalog – The name of the Database.
Integrated Security - By default False. If set true it signifies that Windows Authentication needs to be used.
 
Connection String with SQL Server Authentication
SQL Server Connection String for SQL Server Authentication is defined as follows and it consists of the following properties.
Data Source – The name of the SQL Server and its Instance.
Initial Catalog – The name of the Database.
User Id – The User Id of the SQL Server.
Password – The Password of the SQL Server.
{
 "ConnectionStrings": {
    "WinAuth": "Data Source=.\\SQL2017;Initial Catalog=AjaxSamples; Integrated Security=true",
    "SQLAuth": "Data Source=.\\SQL2017;Initial Catalog=AjaxSamples; User ID=sa;Password=pass1234"
 }
}
 
 
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 with SQL Server Authentication is read from the AppSettings.json file using the GetConnectionString function.
public class HomeController : Controller
{
    private IConfiguration Configuration;
 
    public HomeController(IConfiguration _configuration)
    {
        Configuration = _configuration;
    }
 
    public IActionResult Index()
    {
        string winAuth = this.Configuration.GetConnectionString("WinAuth");
        string sQLAuth = this.Configuration.GetConnectionString("SQLAuth");
        return View();
    }
}
 
 
Screenshot
.Net Core: Set SQL Server Connection String in AppSettings.json
 
 
Downloads