Tuesday 25 August 2020

ASP NET Core appsettings json file

 ASP.NET Core appsettings.json file

Configuration Sources in ASP.NET Core

In previous versions of ASP.NET, we store application configuration settings, like database connection strings for example, in web.config file. In ASP.NET Core application configuration settings can come from the following different configurations sources.

  1. Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)
  2. User secrets
  3. Environment variables
  4. Command-line argument

appsettings.json file : In the asp.net core project that is generated by the "Empty" project template we already have a file with name appsettings.json. I have modified this file to include a new setting with the key - MyKey.

{
    "Logging": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "AllowedHosts""*",
    "MyKey""Value of MyKey from appsettings.json"
  }


Accessing configuration information 

To access configuration information in the Startup class, inject the IConfiguration service provided by the Framework. Startup class is in Startup.cs file.

public class Startup
{
    private IConfiguration _configuration;

    // Notice we are using Dependency Injection here
    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void Configure(IApplicationBuilder appIHostingEnvironment env)
    {
        app.Run(async (context=>
        {
            await context.Response.WriteAsync(_configuration["MyKey"]);
        });
    }

    var ResLogConfiguration.GetSection("ResponseLogging").Value ;
    var CorsHeaderConfiguration.GetValue<string>("Cors:Headers");
}


ASP.NET Core IConfiguration service

  1. IConfiguration service is setup to read configuration information from all the various configuration sources in asp.net core
  2. If you have a configuration setting with the same key in multiple configuration sources, the later configuration sources override the earlier configuration sources 
  3. CreateDefaultBuilder() method of the WebHost class which is automatically invoked when the application starts, reads the configuration sources in a specific order.
  4. To see the order in which the configuration sources are read, please check out ConfigureAppConfiguration() method on the following link github.com

Upon inspecting the file, you will see, the following is the default order in which the various configuration sources are read

  1. appsettings.json, 
  2. appsettings.{Environment}.json
  3. User secrets
  4. Environment variables
  5. Command-line arguments

You can change this order if you want to or even add your own custom configuration sources in addition to all the existing configuration sources. 


0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives