如何解决无法加载基于环境的Appsettings.json
我想阅读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",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","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","environmentvariables": {
"ASPNETCORE_ENVIRONMENT": "Test"
}
},"WICAPI": {
"commandName": "Project","environmentvariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
解决方法
- 首先,您需要在
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","launchUrl": "http://localhost/test","environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},"applicationUrl": "http://localhost:51572/"
},"test.Web.Staging": {
"commandName": "Project","environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
},"test.Web.RC": {
"commandName": "Project","environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "ReleaseCandidate"
},"test.Web.Prod": {
"commandName": "Project","environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},"applicationUrl": "http://localhost:51572/"
}
}
}
- 您的json文件
-
在启动文件中,您可以使用
services.Configure(Configuration.GetSection(“ ApplicationSettings”));
-
运行调试时,选择要运行的
env
希望能为您提供帮助。
, 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"
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。