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

Dotnet Guru Archives