How to read appsettings.json configuration file in .Net Core Console Application

Updated on: March 17, 2021

The following steps will help you create, write and read Appsettings.json configuration file in your .Net Core Console Aplication

Step 1: Create Dot Net Core Console application using Visual Studio or through .NET CLI Command as shown below:

.NET CLI Command:

dotnet new console -o SampleApplication1

Step 2: Once .NET Core Console application is created, create appsettings.json file in the project and add following configuration to the file:

  "ConnectionString": "Server=localhost;Database=tempDB;Uid=<dbUserName>;Pwd=<dbPassword>",
  "Smtp": {
    "Host": "",
    "Port": "587",
    "Username": "<YourGmailUserName>",
    "Password": "<YourGmailPassword>",
    "From": "Your Name"

Step 3: Modify appsettings.json file's "Copy to Output Directory" property to "Copy if newer" option from "Properties" window of Visual Studio as shown below:

Step 4: Now add the below Nuget Packages to your application:

dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Json

Step 5: Write the below code to read the appsetings.json file contents as shown below:

    class Program
        static void Main(string[] args)
              var builder = new ConfigurationBuilder()
               .AddJsonFile($"appsettings.json", true, true);

            var config = builder.Build();
            var connectionString = config["ConnectionString"];
            var emailHost = config["Smtp:Host"];
            Console.WriteLine($"Connection String is: {connectionString}");
            Console.WriteLine($"Email Host is: {emailHost}");

Step 6: Now run the project and see the output, it will read the configuration from appsettings.json file and display it in result as shown below:

As shown above, you can use the above code to configure your application to use the configuration stored in appsettings.json file.