How to Read ConnectionString or AppSettings from appsettings.json in .net Core


In this article we are going to see how to read ConnectionString or AppSettings from appsettings.json in .net Core application

If you are already familiar with .net frameworks than you have noticed that in .net Core, Microsoft has changed the configuration file in web applications. Previously it was web.config which is XML based configuration file, and now in .net core they have replaced web.config with appsettings.json which is JSON  based configuration file.

Explanation

The image you are looking is the image of appsettings.json which is present in .net core application.

Now we will see that how to fetch Connection string and AppSettings

Here I am taking an example of Asp.net Core 3.1 WebApi

Step 1

First you have import namespace

using Microsoft.Extensions.Configuration;

Step 2

Second you have to inject IConfiguration into the Controller’s contructor

Step 3

In this step we are going to fetch Connectionstring of the “DefaultConnection”.

_ configuration.GetConnectionString("DefaultConnection");

Step 4

In this step we are going to fetch AppSetting of the “Token”.

_configuration.GetSection("AppSettings:Token").Value;
A

Snippets

To fetch

connectionstring

_configuration.GetConnectionString("DefaultConnection");

AppSetting

_configuration.GetSection("AppSettings:Token").Value;


Leave a Reply

Your email address will not be published. Required fields are marked *