我想阅读appsettings.json,然后根据每个环境替换密钥。经过大量研究后,我感到无知,我读了其他类似文章,但所提供的解决方案无济于事,Program.cs的实现在理论上应该可以工作,但应用程序仅使用appsettings.json。
Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment}.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
public static string Environment
{
get
{
string environmentName;
#if DEBUG
environmentName = "Development";
#elif RELEASE
environmentName = "Test";
#endif
return environmentName;
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"myconn": "connection"
}
}
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"myconn": "connectionDev"
}
}
{
"ConnectionStrings": {
"myconn": "connectionDev"
}
}
launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49416/",
"sslPort": 44368
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express Test": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Test"
}
},
"WICAPI": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
答案 0 :(得分:2)
launchSettings.json
中创建Properties
对于内容,诸如此类。更新关注您的业务。
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost/test",
"sslPort": 0
},
"iisExpress": {
"applicationUrl": "http://localhost:51572/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"test.Web.Local": {
"commandName": "IIS",
"launchBrowser": true,
"launchUrl": "http://localhost/test",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:51572/"
},
"test.Web.Staging": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
},
"applicationUrl": "http://localhost:51572/"
},
"test.Web.RC": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "ReleaseCandidate"
},
"applicationUrl": "http://localhost:51572/"
},
"test.Web.Prod": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://localhost:51572/"
}
}
}
在启动文件中,您可以使用
services.Configure(Configuration.GetSection(“ ApplicationSettings”));
运行调试时,选择要运行的env
希望能为您提供帮助。
答案 1 :(得分:0)
ASP.NET Core实际上为您做到了。不需要做任何事情。将function addName(obj, name, value) {
return {...obj, [name]: value}
}
addName({}, "Brutus", 300)
保留为默认值:
Program.cs
常规配置必须位于 public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
上,只需在appsettings.json
文件中写入要从appsettings.json
覆盖的配置即可,即环境名称appsettings.{env}.json
,例如:
appsettings.json
{env}
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"myconn": "connection"
}
}