Configuration in asp.net core

Time:2021-7-21

Asp.net core provides a flexible and extensible configuration system based on key value. However, the configuration system is independent of asp.net core. It is a part of microsoft.extensions class library. It can be used for any type of application

1. Read the configuration as a key value pair

Appsettings.json file:

{
    "Position": {
        "Title": "editor,",
        "Name": "Joe Smith"
    },
    "MyKey": "My appsettings.json Value",
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*"
}

Add the following test code to the configureservices method:

var myKeyValue = Configuration["MyKey"];
        var title = Configuration["Position:Title"];
        var name = Configuration["Position:Name"];
        var defaultLogLevel = Configuration["Logging:LogLevel:Default"];

2. Multi environment configuration

Using the default configuration, the environment variables configuration provider loads the configuration from the environment variable key value pair after reading appsettings.json, appsettings.environment.json and the secret manager. Therefore, the key values read from the environment replace the values read from appsettings.json, appsettings.environment.json, and the secret manager. The environment variables set in launchsettings.json will replace those set in the system environment.

3. Read structured configuration data

Add a class testsubsectionconfig corresponding to the subsection node in the configuration file

public class TestSubSectionConfig
    {
        public string SubOption1 { get; set; }
        public string SubOption2 { get; set; }
    }

Add the following test code to the configureservices method:

//Use getsection to resolve the section of the configuration file
var subsectionOptions = Configuration.GetSection("subsection").Get();
var suboption2 = subsectionOptions.SubOption2;

Console.WriteLine($"subsection:suboption2: {suboption2}");

If you need to use it in the controller, you can use dependency injection:

Register configuration items in configuration services.

public void ConfigureServices(IServiceCollection services)
{
    //Register configuration to service container
    services.Configure(Configuration.GetSection("subsection"));

    //var subsectionOptions = Configuration.GetSection("subsection").Get();
    //services.Configure(options =>
    //{
    //    options.SubOption1 = subsectionOptions["suboption1"];
    //    options.SubOption2 = subsectionOptions["suboption2"];
    // });

}

public class HomeController : Controller
{
    private TestSubSectionConfig _subSectionConfig;
    private ILogger _logger; 

    public HomeController(IOptions option, ILogger logger)
    {
        _subSectionConfig = option.Value;
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation($"SubOption1: {_subSectionConfig.SubOption1}");
        _logger.LogInformation($"SubOption2: {_subSectionConfig.SubOption2}");
        return View();
    }
}