asp.net-coreappsettings

Change ASP.NET Core application configuration sources order of precedence


I understand what the default order of preference is for ASP.NET Core as mentioned here MS Docs | Default application configuration sources. However I would like to know if it is possible to change this order?

In my scenario, all app settings are defined as Env variables in a deployment.yaml manifest which is stored in a remote manifests repo used by a GitOps operator (ArgoCD) to deploy the application.

However, when working on a feature branch, I want to be able to override those env settings with the appsettings.preview.json file that is in the app source code repo, thus avoiding changes in the app manifest repo when working on features. Changes to that remote repo will only be done when testing is complete and we can define the required values in the env variables of the deployment.yaml manifest.

I was planning to deploy the feature branch version with ASPNETCORE_ENVIRONMENT=Preview and only change the order based on this value being set.


Solution

  • I suggest you could consider check the environment firstly and manually add the config appsetting.json file as below:

    var env = builder.Environment;
    
    if (env.EnvironmentName == "Preview")
    {
        builder.Configuration.AddJsonFile("appsettings.preview.json", optional: true, reloadOnChange: true);
    
    }
    else
    {
        builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
    }