Detailed explanation of configuration in ASP. NET Core

  • 2021-11-24 01:17:30
  • OfStack

ASP. NET Core provides a flexible, extensible, key-based configuration system. However, the configuration system is separate from ASP. NET Core, which is part of the 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

With the default configuration, EnvironmentVariablesConfigurationProvider 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, the environment variables set in launchSettings. json will replace the variables set in the system environment.

3. Read structured configuration data

Add 1 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 Parsing a section of a configuration file 
var subsectionOptions = Configuration.GetSection("subsection").Get<TestSubSectionConfig>();
var suboption2 = subsectionOptions.SubOption2;

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

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

Register configuration items in ConfigureServices.


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

  //var subsectionOptions = Configuration.GetSection("subsection").Get<TestSubSectionConfig>();
  //services.Configure<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 = option.Value;
    _logger = logger;
  }

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


Related articles: