SoFunction
Updated on 2025-04-07

Detailed explanation of the configuration in Core

Core provides a flexible and scalable, key-value-based configuration system. However, the configuration system is independent of Core as part of the class library. It can be used for any type of application.

1. Read the configuration in the form of key-value pairs

document:

{
  "Position": {
    "Title": "Editor",
    "Name": "Joe Smith"
  },
  "MyKey": "My  Value",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "": "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

With the default configuration, EnvironmentVariablesConfigurationProvider loads the configuration from environment variable key-value pairs after reading , and secret managers. Therefore, key values ​​read from the environment will replace values ​​read from , and confidential managers. The environment variable set in , the environment variable set in , will replace the variable 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 parse the section of the configuration filevar subsectionOptions = ("subsection").Get<TestSubSectionConfig>();
var suboption2 = subsectionOptions.SubOption2;

($"subsection:suboption2: {suboption2}");

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

Register the configuration item in ConfigureServices.

public void ConfigureServices(IServiceCollection services)
{
  //Register and configure to the service container  <TestSubSectionConfig>(("subsection"));

  //var subsectionOptions = ("subsection").Get<TestSubSectionConfig>();
  //<TestSubSectionConfig>(options =>
  //{
  //  options.SubOption1 = subsectionOptions["suboption1"];
  //  options.SubOption2 = subsectionOptions["suboption2"];
  // });

}

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

  public HomeController(IOptions<TestSubSectionConfig> option, ILogger<HomeController> logger)
  {
    _subSectionConfig = ;
    _logger = logger;
  }

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

This is the end of this article about the detailed explanation of the configuration in Core. For more related Core configuration content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!