简体   繁体   中英

ASP.NET Core appsettings.json not loading the correct settings

In my Azure I have ENVIRONMENT = Development , but my settings don't load.

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
       .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
       .AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
       .Build();

But it always uses the default settings.

请更新您的以下行:

.SetBasePath(env.ContentRootPath)

This is how I config my startup.cs when I deploy to azure

public Startup(
    IConfiguration configuration,
    IHostingEnvironment hostingEnvironment)
{
    _configuration = configuration;
    _hostingEnvironment = hostingEnvironment;

    var builder = new ConfigurationBuilder();

    if (_hostingEnvironment.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }
    else
    {
        builder
            .SetBasePath(hostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }
}

Update: Try this code in Program.cs

private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;

public Program(
    IConfiguration configuration,
    IHostingEnvironment hostingEnvironment)
{
    _configuration = configuration;
    _hostingEnvironment = hostingEnvironment;

    var builder = new ConfigurationBuilder()
            .SetBasePath(hostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
}

Let me know if you still have any problem

In my program.cs I did

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
            .Build();

than I did

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args) // Sets up the default order in which all the configurations are read
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((c, x) =>
            {

                x.AddConfiguration(Configuration); <-------

            })
            .UseSerilog((h, c) => c.Enrich.FromLogContext().WriteTo.Sentry(s =>
            {

                s.Dsn = new Sentry.Dsn(Configuration.GetSection("Sentry:Dsn").Value);
                s.MinimumEventLevel = Serilog.Events.LogEventLevel.Error;
                s.MinimumBreadcrumbLevel = Serilog.Events.LogEventLevel.Information;

            })).UseSentry(x =>
            {
                x.IncludeRequestPayload = true;
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM