Base solution for your next web application
Open Closed

How can we configure our Angular & ASP.NET Core application to load settings from a specific subfolder when the environment variable IsContainer #12162


User avatar
0
pliaspzero created

Dear ASP.NET Zero Support Team,

I hope this message finds you well.

I am currently working on our ASP.NET Core & Angular project and have a couple of questions regarding the configuration of our appsettings.json files for container deployments.

Dynamic Configuration Loading: How can we configure our ASP.NET Core application to load settings from a specific subfolder when the environment variable IsContainer is set to true? We want to ensure that the application correctly uses this configuration in a containerized environment.

New Folder for Configuration Files: For both the ASP.NET Core backend and the Angular frontend, we would like to create a new, dedicated folder solely for appsettings*.json files. Instead of loading these configurations from the /assets folder in the Angular project, we want a separate clean folder. What steps should we follow to implement this structure in both projects?

Thank you for your assistance!


2 Answer(s)
  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @pliaspzero,

    Please look at these documents. I think it should help. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0 https://www.roundthecode.com/dotnet-tutorials/how-to-read-the-appsettings-json-configuration-file-in-asp-net-core

  • User Avatar
    0
    pliaspzero created

    ok - thanks - in ASPZERO - could I do it here?

    public static class AppConfigurations
    {
        private static readonly ConcurrentDictionary<string, IConfigurationRoot> ConfigurationCache;
    
        static AppConfigurations()
        {
            ConfigurationCache = new ConcurrentDictionary<string, IConfigurationRoot>();
        }
    
        public static IConfigurationRoot Get(string path, string environmentName = null, bool addUserSecrets = false)
        {
            var cacheKey = path + "#" + environmentName + "#" + addUserSecrets;
            return ConfigurationCache.GetOrAdd(
                cacheKey,
                _ => BuildConfiguration(path, environmentName, addUserSecrets)
            );
        }
    
        private static IConfigurationRoot BuildConfiguration(string path, string environmentName = null,
            bool addUserSecrets = false)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(path)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
            if (!environmentName.IsNullOrWhiteSpace())
            {
                builder = builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true);
            }
    
            builder = builder.AddEnvironmentVariables();
    
            if (addUserSecrets)
            {
                builder.AddUserSecrets(typeof(AppConfigurations).GetAssembly(), true);
            }
    
            var builtConfig = builder.Build();
            new AppAzureKeyVaultConfigurer().Configure(builder, builtConfig);
    
            return builder.Build();
        }