2

I have developed a web API and published it on azurewebsites.net. I have added the following additional appsettings:-

  1. appsettings.Dev.json
  2. appsettings.Test.json
  3. appsettings.Prod.json

To be able to extract values from these appsettings transforms I made the following code changes:

Tried the solution mentioned here: https://stackoverflow.com/a/44953524/10485667

Even tried using only the Development/Debug, Staging and Production/Release instead of Dev, Test, Prod receptively. But no luck. It would only publish the values from the main appsettings.json.

Startup.cs

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
#if DEBUG
                    .AddJsonFile($"appsettings.Dev.json", optional: true)
#endif
                    .AddEnvironmentVariables();

        Configuration = builder.Build();
        appSettings = Configuration.Get<AppSettingsModel>().AppSettings;

    }

even tried this code:

AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)

Tried changing the Program.cs:

 public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
       .AddJsonFile($"appsettings.Development.json", optional: true)
       .Build();
        ILogger logger = null;
        var host = CreateWebHostBuilder(args)
            .UseConfiguration(config)
            .Build();
        logger = host.Services.GetService<ILogger>();
        host.Run();
    }
}

Tried every possible solution provided on internet but no luck. After publishing to azure, it takes the values only from appsettings.json

I think I might be making some conceptual mistake while attempting these solutions. Any kind of help is appreciated.

Thanks in advance

dokyalashot
  • 105
  • 2
  • 18
  • In your third example you have `.Development.`, if that's a typo from `.Dev.`. Also `env.EnvironmentName` is the value of the `ASPNETCORE_ENVIRONMENT` environment variable, which defaults to "Production" if not set. – gunr2171 Jun 18 '19 at 19:10
  • You don't mention it in the question, but are you sure your environmental variables are setup on whatever environment you're running this on? – Mkalafut Jun 18 '19 at 19:11
  • 1
    If you do not explicitly set the `ASPNETCORE_ENVIRONMENT` environment variable to something, it will revert to `Production`, as stated by @gunr2171. For Azure App Service, you need to create a new Application Setting named `ASPNETCORE_ENVIRONMENT` and set its value to `Development` (or whatever you need). – Federico Dipuma Jun 18 '19 at 19:14
  • 3
    @FedericoDipuma: Yes, I did that.. but I found where I was going wrong. I did not make the appsettings.{env}.json 's 'Copy to OutputDirectory" to Copy Always. After making that change it worked – dokyalashot Jun 18 '19 at 20:01

0 Answers0