考虑以下
为了成功读取
现在,假设键名为
如何修改
appsettings.json
文件:{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"NumberOfRetries": 5,
"Option1": "abc",
"Option2": "def"
}
为了成功读取
NumberOfRetries
,可以使用以下类:public class AppSettings
{
public int NumberOfRetries { get; set; }
public string Option1 { get; set; }
public string Option2 { get; set; }
}
以下是在Startup.cs中的代码:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddOptions();
services.Configure<AppSettings>(Configuration);
}
现在,假设键名为
Number.Of.Retries
而不是用下划线进行分隔的NumberOfRetries
。如何修改
AppSettings
类(或方法本身)以支持这种情况?无法在属性名称中使用句点。