门户中的Azure应用服务应用设置不会覆盖appsettings.json值

时间:2019-12-12 20:45:42

标签: .net azure asp.net-core appsettings

我在“启动”(asp.net core 2.2项目)中具有以下内容:

 public Startup(IConfiguration configuration, IHostingEnvironment environment)
 {
        Configuration = configuration;
        Environment = environment;

        new ConfigurationBuilder()
            .SetBasePath(Environment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
 }

 public void ConfigureServices(IServiceCollection services)
 {
        var appSettings = new AppSettings();

        Configuration.Bind("appSettings", appSettings);

        services.AddSingleton(appSettings);

        ....
}

我已经设置了值以覆盖appsettings.json中的所有应用程序设置值,但是我的应用程序服务仍在使用appsettings中的内容,而不是我在门户中输入的内容。根据文档,应用程序服务的那些门户网站应用程序设置应覆盖appsettings.json文件并被使用。我错过了一步吗?

谢谢

编辑:

即使将“启动”更改为以下内容也无法使用我的Azure应用设置:

    public Startup(IConfiguration configuration, IHostingEnvironment environment)
    {
        Configuration = configuration;
        Environment = environment;

        Configuration = new ConfigurationBuilder()
            .AddConfiguration(configuration)
            .SetBasePath(Environment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }

3 个答案:

答案 0 :(得分:0)

尝试致电.UseStartup<Startup>()

Configuration = new ConfigurationBuilder()
            .AddConfiguration(configuration)
            .SetBasePath(Environment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true)
            .AddEnvironmentVariables()
            .Build()

.UseStartup<Startup>();

答案 1 :(得分:0)

要在门户网站上获取Appsettings时,需要使用AppSettings:Hello之类的名称来引用appsettings.*.json文件中复杂结构内的变量名。请参阅此article。以下是您可以参考的步骤。

HomeController:

private AppSettings AppSettings { get; set; }
public HomeController(IOptions<AppSettings> appSettings)
{
    AppSettings = appSettings.Value;
}
public IActionResult Index()
{
    ViewBag.Message = AppSettings.Hello;
    return View();
}

AppSettings.cs:

public class AppSettings
{
    public string Hello { get; set; }
}

Startup.cs:

public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
    Configuration = configuration;
    Configuration = new ConfigurationBuilder()
        .AddConfiguration(configuration)
        .SetBasePath(environment.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables()
        .Build();
}
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}

appsettings.json:

"AppSettings": {
    "Hello": "world"
}

在门户网站上:

enter image description here

输出:

enter image description here

答案 2 :(得分:0)

WindowsLinux 容器略有不同。

对于以下设置:

"AppSettings": {
    "Hello": "world"
}

Windows 键: AppSettings:Hello

Linux 密钥: AppSettings__Hello