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

Appsettings.json file in ASP.NET Core


In this article, We are  going to discuss the use and importance of the ASP.NET Core appsettings.json file in detail in visual studio.In the Asp.Net Core application we may not found the web.config file it's replace by "appsettings.json". So to configure the settings like database connections string,kyes,values,Mail settings or some other custom configuration settings we will use the "appsettings.json". 

 Different Configuration Sources available in the ASP.NET Core

In ASP.NET Core, the application configuration settings can be stored in different configurations sourece,different key,value such as appsettings.json file, appsettings.{EnvironmentName}.json file (where the {Environment} is the application's current hosting environments such as Development, Staging or Production), User Secrets (where we used to store sensitive information) etc.The application configuration settings can come from different configurations sources such as

  • Appsettings.json File: (appsettings.json, appsettings.{Environment}.json, where the {Environment} is the nothing but the applications current hosting environments such as Development, Staging or Production)
  • Environment Variable
  • User secrets
  • Command-line arguments

Add New Keys

The appsettings.json file can be configured with Key and Value pair combinations. So, the Key will have single or multiple values.Appsettings.json always contain values with key value pair separated with a colon (" ").

{
  "ConnectionStrings": {
    "windowsAuthentication": true,
    "DefaultConnection": "Data Source=sql-server-name;Database=database-name; uid=sql-id;pwd=sql-password;",    
  },


How to access the configuration information appsettings.json?

Red the configuration information within the Startup class or direct your controller,need to use the IConfiguration service which is provided by the ASP.NET CoreThe best  simple and easy way to access the configuration information from appsettings.json in Asp.net core is by using the IConfiguration using the namesapce Microsoft.Extensions.Configuration.

In HomeController constructor we need to set the local IConfiguration property like given below.

From direct in controller

using Microsoft.Extensions.Configuration;
public class HomeController : Controller
{
    private readonly IConfiguration config;
    public HomeController (IConfiguration configuration)
    {
    config = configuration;
    }
    public IActionResult index()
    {
    // First method to access the configuration 
    string _connection = config.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;
    // Second method to access the configuration 
    string _connection1 = config.GetValue<string>("ConnectionStrings:DefaultConnection");
    return View();
    }
}

From Startup.cs class

To access appsettings.json from startup.cs class

Explanation of the above code:

First, we created a private variable of type IConfiguration config [This IConfiguration interface belongs to Microsoft.Extensions.Configuration namespace, so bring this namespace first]. Then through constructor dependency injection we inject the IConfiguration object and store it within the private variable config. The following code exactly does this.

    private readonly IConfiguration config;
    public HomeController (IConfiguration configuration)
    {     config = configuration;
    }

Set the AspNetCoreHostingModel value to InProcess in the application’s project file as shown below.

<Project Sdk="Microsoft.NET.Sdk.Web">
 <PropertyGroup>
  <TargetFramework>netcoreapp5.0</TargetFramework>
  <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
 </PropertyGroup>
</Project>

Summary 

In this blog, We discussed how we can add the configuration in appsettings.jsong through Key-Value pairs and how to access  the configuration from appsettings.json file in Asp.net Core using the extensions IConfiguration and IOptions.

Mahira  khanna

Mahira khanna

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

Comments



Report Response