Dynamically Add Connection String To ASP.NET Core appSettings.json


In this article, you will learn how to add the dynamic database connection string. in asp.net core MVC using the appSettings.json file. So, we will understand how to set up load configuration in our Startup.cs class. from an appSettings.json file and appSettings.production.json file.

Create An New application

Now, Let’s, go to visual studio and we’ll create a new ASP.NET Core web application in the empty one. we’re going to add configuration settings.

asp.net core project structure

Let’s open the StartUp.cs file and we want to create a constructor. and we’re also going to create a property and the property is going to be IConfiguration. then, we’ll make sure that the using statement Microsoft extensions configuration has been added up the top. After that, we’re going to add to our constructor and also add a configuration builder that’s going to allow us to set the base path and our JSON files and some other settings.

startup.cs in asp.net core mvc

So, we’re going to add a route path and also going to need to add the hosting environment at the top and our startup parameters and it’s going to tell us what environment that we’re in so that we can see it different settings for different environments whether there be local or production.

we have added JSON file and make it optional false and reload on change true.

So, that means that it’s a needed file, and if the application on The startup cannot find it will crash. we’re are already added multiple JSON files in our configuration builder and our next one is going to be it settings {env.EnvirnmentBase}.json and we’ll make this one optional true. so what that’s going to allow us to do is create appSettings.json files.

Add appSettings.json file

Now, we need to add our appSettings.json files. so, we going to right click on the project and add new Item.

add appsetting.json in asp.net core mvc

here, you can see the appSettings.json file with connection string. connection string in asp.net core mvc.

and also going to add the production appSettings.
add appsetting.production file in asp.net core

the appSettings get loaded upon startup. and in the environment variables or any variables that have been set in the appSettings.json file. the production will overwrite anything in appSettings will be used in the production environment.

so, we have added some temporary values to our appSettings.json file as you can see these are just example.

appsetting.production in asp.net core

It show’s how the hierarchy of the appSettings works. and how we can exist them. So, we’ve got an implication settings base URL application name and environment development.

so, test the settings different ways that we can read these values. let’s look at how we can read directly from the appsettings.JSON file

Let’s have a look want to see what environment we’re in configuration and we’re going to follow the path.

add configuration in appsetting.json file

We put a breakpoint there and we run it.

debugging in asp.net core mvc

Reading properties using model class

In our appSettings.json file. another way that wants to use their appSettings is by reading it through our application. So, Let’s look at how we’ll do that.

Let’s first create a model folder and create a class called appSettings.cs

create appsetting.json in asp.net core mvc

We’re going to create a property named environment. and then load up our appSettings.cs class file. here, with all the settings and our app settings JSON file and then we can inject their end-user into different classes throughout our application.

create appsetting.cs class in asp.net core

Add Service to Container

Now, that we’re going to appSettings file in it. we need to inject it into our ConfigureServices() method.

ConfigureServices register appsetting class

Let’s configure the class that we created app settings. now, configuration get a section. because we want to get a configuration section.

The application settings here directly relates to our sitting into JSON file application settings. so, it’s going to load up all of these as long as we have properties for them at the moment we only have the environment.

properties in appsetting.json

Add Controller

Let’s create a simple controller that returns appSettings values. then load up appSettings inside your application.

we need to create a field up top private read-only appSettings.json. we also want to load this up in the constructor and we’re going to use IOptions.

read appsetting properties in asp.net core

Let’s look at how to test the appSettings.JSON file. we don’t necessarily want to load this up into our production environment. to test their all of these settings are working. so, if we get along to debug appSettings properties we can change under debug the value.


Leave a Reply

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