gpt4 book ai didi

azure - 我的 Azure Web 应用程序无法从正确的 appsetting.{env}.json 文件中读取参数

转载 作者:行者123 更新时间:2023-12-03 02:13:39 25 4
gpt4 key购买 nike

我开发了一个 Azure Web 应用程序 (dotnetcore 3.1),并为每个环境添加了 appsettings 文件,例如

  • appsettings.json
  • appsettings.Prod.json
  • appsettings.Staging.json
  • appsettings.Development.json

它按预期在我的本地工作,但当我将其部署到 Azure 门户时,它始终从 appsettings.json 文件中读取参数,即使我将 ASPNETCORE_ENVIRONMENT = Staging 添加到 azure 应用程序设置也是如此。在这种情况下,我希望它应该从 appsettings.Staging.json 文件中读取参数。

我会错过什么?知道可能出什么问题吗?提前致谢...

MVC Web 应用

enter image description here

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", EnvironmentVariableTarget.Machine);
IConfigurationBuilder cb = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{envName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();

IConfiguration cfg = cb.Build();

services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddSingleton(cfg);
services.AddMemoryCache();
services.AddSession();
services.AddMvc();
}

HomeController.cs

public IActionResult Index()
{
var key = _configuration["Settings:firstKey"].ToString();
ViewBag.FirstKey = key;
return View();
}

appsettings.json

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Settings": {
"firstKey": "from appsettings.json"
}
}

appsettings.Staging.json

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Settings": {
"firstKey": "from appsettings.Staging.json"
}
}

appsettings.Development.json

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Settings": {
"firstKey": "from appsettings.Development.json"
}
}

appsettings.Prod.json

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Settings": {
"firstKey": "from appsettings.Prod.json"
}
}

azure 应用程序设置 enter image description here

最佳答案

我通过更新program.cs和startup.cs文件解决了我的问题,如下所示。

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
// removed appsettings configuration in startup.cs file
services.AddMemoryCache();
services.AddSession();
services.AddMvc();
services.AddControllersWithViews();
}

Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.ConfigureAppConfiguration((config)=> {
var appSetting = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", EnvironmentVariableTarget.Machine)}.json";
config.AddJsonFile(appSetting, optional: true, reloadOnChange: true);
});
});

我猜 Web 应用程序在部署到 Azure 门户时首先无法读取startup.cs 中的环境变量

结果

enter image description here

关于azure - 我的 Azure Web 应用程序无法从正确的 appsetting.{env}.json 文件中读取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72274990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com