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

Related searches

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.

Please update your below line:

.SetBasePath(env.ContentRootPath)

Why isn't my ASP.NET Core environment-specific configuration , Hi there I'm trying to do a basic integration test for a very simple ASP.NET Core WebApi project. I'm leveraging the WebApplicationFactory� By default, Secret manager reads configuration settings after appsettings.json and appsettings.Environment.json. For more information on storing passwords or other sensitive data: Use multiple environments in ASP.NET Core; Safe storage of app secrets in development in ASP.NET Core: Includes advice on using environment variables to store

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

Unable to load/reference appsettings.json when integration testing , I'm afraid of being that appsettings.Development.json does not load properly. When I debug my aspnetcore application with� You may remember <AppSettings from .NET Full, and luckily, ASP.NET Core (and .NET Core) have a similar construct. In short, you create a new file named appsettings.json and start adding application settings as JSON properties. For the full picture, check out this blog post: AppSettings in ASP.NET Core. A valid appsettings.json file could look

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;
            });

appsettings.Development.json does not load properly � Issue , In ASP.NET Core, your web.config file with its <appsettings> section is gone. The component that isn't replaced by the Startup class, however, is the appsettings While appsettings.json holds your settings, to retrieve those settings lay out the main line of working with appsettings.json, IConfiguration,� In today’s article, we will see how to read values from appsettings.json in ASP.NET Core similarly to what we used to do in ASP.NET MVC by reading values from web.config. Tune in FREE to the React Virtual Conference Sep. 11 at 10am ET x

Configuration in ASP.NET Core, Setting the environment to "development" instead of "Development" will result in your configuration files not loading on a case-sensitive OS like Linux. ASP.NET Core environments. ASP.NET Core has the concept of environments, which represent the different locations your code might be running. You can determine the current environment at runtime

Accessing Configuration Settings in ASP.NET Core -- Visual Studio , Re: ASP.NET CORE 2: Using Correct appsettings.json Per Environment Apr 17, 2018 12:01 AM | polymorphic | LINK Ok, so what I seem to be seeing is that, regardless of the machine's environment variable setting for ASPNeTCORE_ENVIRoNMENT, what matters with this code base is what profile I use to build the launchSettings.json.

One uses IOptions in ASP.NE Core solutions to read appsettings.json file into a class/object that can be used by another class/object that is done with Core's IoC services passing a class/object into another class/object by means of dependency injection via a class/object's constructor, which is called constrictor injection in using dependency injection used by .NET Core.

Comments
  • Have you ever tried setting ASPNETCORE_ENVIRONMENT?
  • How and where did you set the environment variable?
  • How can I set this in my program.cs, its where am doing everything
  • I do stuff like this in my program.cs, how can I do that?