根本原因是 IConfiguration 未通过 DI 容器注入:普通类需注册到 DI 并通过构造函数接收 IConfiguration;GetSection 返回 null 多因键名大小写或冒号分隔错误;Get
IConfigurationIConfigurationnewappsettings.jsonIConfigurationProgram.csservices.AddScoped() WebHostEnvironmentJsonConfigurationProviderappsettings.Development.jsonGetSection("Logging"){"ConnectionStrings": {"Default": "..."}}ConnectionStrings:DefaultConnectionStrings.DefaultConnectionStrings/DefaultGetSectionconfig.GetSection("connectionstrings")ConnectionStringsconfig.GetSection("AppSettings:Title"){"AppSettings": {"title": "..."}}titleCache:Redis:HostCache:HostConsole.WriteLine(string.Join(", ", config.AsEnumerable().Select(kv => kv.Key)));Get() Get() [JsonPropertyName]0nullfalse[JsonPropertyName("my_key")]"redis_host"public string RedisHost { get; set; }ConfigureOptionsJsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCaseGetSection("xxx").GetChildren()appsettings.Development.jsonappsettings.jsonDevelopmentIConfigurationappsettings.jsonappsettings.{Environment}.jsonappsettings.*.jsonCopyToOutputDirectoryPreserveNewestASPNETCORE_ENVIRONMENTset ASPNETCORE_ENVIRONMENT=Productionexport ASPNETCORE_ENVIRONMENT=ProductionEnvironment.EnvironmentNameIHostingEnvironment