无法加载基于环境的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

enter image description here

对于内容,诸如此类。更新关注您的业务。

    {
  "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文件

enter image description here

  • 在启动文件中,您可以使用

    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"
  }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...