Introduction
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.
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.
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.

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

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.

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.

We put a breakpoint there and we run it.

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
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.

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

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.

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.
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.
Good respond in return of this issue with solid arguments and explaining everything on the topic of that.
Pretty! This has been an extremely wonderful post.
Many thanks for providing this info.
I appreciate you sharing this blog article.Really looking forward to read more. Really Cool.
I am so grateful for your blog.Really thank you! Cool.
Hello!I wish to say that this post is awesome, great written and come with approximately all important infos.I’d like to look extra posts like
this! 🙂