background

ASP.NET Core provides a flexible, extensible, key-value based configuration system. But the configuration system is part of the Microsoft.Extensions library, independent of ASP.NET Core. It can be used with any type of application.

1. Read configuration as key-value pairs

Appsettings. Json file:

{
    "Position": {
        "Title": "编辑器",
        "Name": "Joe Smith"
    },
    "MyKey": "My appsettings.json Value",
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*"
}
Copy the code

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"];
Copy the code

2. Multi-environment configuration

Using the default configuration, EnvironmentVariablesConfigurationProvider will read appsettings. Json, appsettings. Environment. The json and classified after the manager from Environment variable keys to load configuration. Read from the Environment, therefore, the key value will replace the appsettings. Json, appsettings. Environment. The json and confidential returns the value of the manager. Launchsettings. json environment variables set in launchsettings. json will replace variables set in the system environment.

3. Read structured configuration data

Add a class TestSubSectionConfig that corresponds to the Subsection node in the configuration file

public class TestSubSectionConfig { public string SubOption1 { get; set; } public string SubOption2 { get; set; }}Copy the code

Add the following test code to the ConfigureServices method:

Subsection: var subsectionOptions = configuration.getSection (" Subsection ").get <TestSubSectionConfig>(); var suboption2 = subsectionOptions.SubOption2; Console.WriteLine($"subsection:suboption2: {suboption2}");Copy the code

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

Register configuration items in ConfigureServices.

Public void ConfigureServices(IServiceCollection Services) {// Register the configuration to the 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(); }}Copy the code