find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

How to Read Values From appsettings.json in .NET Core C#


In the article, I will see how to read values from appsettings.json in .NET 5 Core similar to what we used to do in ASP.NET MVC Razor  by reading values from web.config.In this article, we will learn how to add new keys a nd values in Appsettings.json  file and read values from appsettings json file in .NET 5 Core application.

In the all versions of ASP.NET, we see config files all types of From WebForm, ASPX,MVC Rzor etc we have a web.config file format in XML syntax, this file is very important to config value for some package install,connectionsstring,version, and so on custom config value to use when an application running.

In .Net Core we have also config file with the same purpose which is bettter and flexible in an appsettings.json format. appsetting .json is the JSON syntax we can create many keys and values in appsettings.json files for many environments like Development, Testing, Product etc.In this article,try to best way to read the value from appsettings.json file using c#.

Database connection example in asp.net core

Create New Application in visual studio 2019

  1. Open your visual studio 2019 less then 2019
  2. After that click on Create a New Project
  3. Search ASP.NET Core Web App(Model-View-Controller) and next
  4. Provide a Project name and confirm or change the Location and next
  5. Select the latest version of ASP.NET Core in the drop-down (.NET Core 5.0)and then select Authentication Type from drop-down
  6. Under Authentication Type, select Change and set the authentication to Individual User Accounts and then click on Create button

Now, I will open the appsettings.jsonfile and add  keys with their values like as given below.

"DemoAppSetting": {  
    "FirstKey": "FirstValue",
    "SecondKey": "SecondValue",
    "ThirdKey": "ThirdValue"
  }  


Now, In My Controller that I have created earlier, I will add a method so that we can test our code.

[HttpGet]  
public IActionResult GetStaffList()
{
var StaffList= new List<string>();
       StaffList.Add("Sylvia");
       StaffList.Add("Rokina");
       return Ok(StaffList);
 }  

Now, in My Controller, I will add a private field of type “IConfiguration” which is found in Microsoft.Extensions.Configuration namespace, so i iwant to  to add that namespace in my file.

Now, I will create a constructor of  my Controller with input parameter “IConfiguration”.

        public HomeController(IConfiguration configuration)
        {           _configuration = configuration;
        }



First Method

namespace CCBCCEMAIL.Controllers
{   
    public class HomeController : Controller
    {         public IConfiguration _configuration;
        public HomeController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public ActionResult FirsMethod()
        {
            string firstValue = _configuration.GetSection("DemoAppSetting:FirstValue").Value;
            string secondValue = _configuration.GetSection("DemoAppSetting:SecondValue").Value;
            string thirdValue = _configuration.GetSection("DemoAppSetting:ThirdValue").Value;
            return View();
        }
    }
}

Second Method

string secondMethod= _configuration.GetSection("DemoAppSetting").GetSection("FirstValue").Value;

In the secondMethod method, you can see the vaules are getting from the configuration object. Inside this, we are getting another section that contains the value.

or

string anotherMethod= _configuration.GetValue<string>("DemoAppSetting:FirstValue");  

In the anotherMethod method,The value is directly getting the string type value from the configuration object. We are separating our nested sections by “:”.


how to get connection string in asp.net core

Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

Comments



Report Response