ASP.NET CORE Configuration

In this tutorial will discuss different ways of storing the configuration and read the configurations. We will discuss below point step by step.

      • Different ways of storing configuration.
      • Default Configuration.
      • Adding Configuration Files.
      • Read Configuration.

In ASP.NET CORE, the WEB.CONFIG file was totally out of use, but if you want to Host an ASP.NET CORE Web Application on IIS, you must need to still use it. 

Different ways of storing configuration

Instead of web.config, The configuration of an ASP.NET Core application is based on a list key-value pairs collected at runtime from a variety of data sources. Such as

      1. Settings Files (JSON(),XML, INI)
      2. Command-line arguments
      3. Environment variables
      4. In-memory .NET objects
      5. Azure Key Vault
      6. We can also create custom providers and add them into our ASP.NET Core application.

You might be worried about how to manage to add files and read data from different sources, don’t worry, all values from different data sources are loaded and composed into a single container. Configuration data is commonly built in the constructor of the startup.cs class.

Default app configuration

Before making any changes for configuration, we have added key in json file

appSettings.json

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"MyJSONKey": "JSON Value"
}

Lets try to read value we are getting value from json file but if you noticed we have not did any changes for loading configuration. This is because the feature called default configuration which is available on asp.net core 2.2 and above version. Now lets where that default configuration is loading.

Default configuration is done during the process of building web host, inside of Program.cs in CreateDefaultBuilder method.

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});
}

Magic happens at line 9, this is where default builder is created. That means where default configuration is constructed. That includes app configuration, logging, default server and few other settings.

Adding Configuration Files

We already have appettings.json in our projects lets add xml and ini file to understand configuration.

public class Startup
{
public IConfigurationRoot Configuration { get; }
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddIniFile("MyIniConfig.ini", optional: true, reloadOnChange: true)
.AddXmlFile("MyXMLFile.xml", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddInMemoryCollection(new Dictionary<string, string> { { "MyMemoryKey", "In Memory object" } });
Configuration = builder.Build();
}

}

Read Configuration

To read configuration data programmatically, you can use an indexer syntax and specify a case-insensitive path string that points to the information you want to read. To delimit properties in a hierarchical schema, you use the colon (:) symbol. For example, consider the following JSON file:

The simplest way to read the MyJson value setting is the following.

var myJSONKeyValue = Configuration["MyJSONKey"];

It is important to note that, by default, the setting is returned as a plain string and must be programmatically converted to its actual concrete type before further use. There’s also a strongly-typed API so we use GetValue

string myJSONKeyValue = Configuration.GetValue("MyJSONKey");

The GetSection method also lets you select an entire configuration subtree where you can act on using both the indexer and the strongly-typed API.

var logLevel = Configuration.GetSection("Logging:LogLevel")
.GetValue("Default");

Note that all the methods for direct access work on the DOM as a whole, regardless of the actual source of the data, whether JSON, memory, command line or whatever.

You can watch our video version of this tutorial with step by step explanation.