参考的appsettings.json
{ "ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.IdentityDb;User Id=sa;Password=Pass@word;", "IsClusterEnv": "False", "MvcClient": "http://localhost:5100", "SpaClient": "http://localhost:5104", "XamarinCallback": "http://localhost:5105/xamarincallback", "UseCustomizationData": false, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Trace", "System": "Information", "Microsoft": "Information" } }, "ApplicationInsights": { "InstrumentationKey": "" }, "UseVault": false, "Vault": { "Name": "eshop", "ClientId": "your-clien-id", "ClientSecret": "your-client-secret" }}
获取方式
1. Configuration["ConnectionString"]//直接获取是这个名称key的值
2. Configuration.GetValue<string>("IsClusterEnv")
3. Configuration["Vault:Name"] //获取嵌套的属性的,多级需要写多个,推荐第一种
4. Configuration.GetSection("Logging")
5. services.Configure<AppSettings>(Configuration);//实体类对象封装,json转换成对象
publicclassAppSettings
{
publicstring MvcClient { get; set; }
publicbool UseCustomizationData { get; set; }
}
Configuration提供的几种封装方式