Appsettings.json based on environments doesn't load

2.2k views Asked by At

I want to read appsettings.json then replace the keys based on each environment. After a lot of research I feel clueless, I read other posts about something similar but the solutions provided didn't help, the implementation in Program.cs should work in theory but the application keeps using just appsettings.json.

Program.cs

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostContext, config) =>
                {

                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{Environment}.json", optional: true, reloadOnChange: true);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

        public static string Environment
        {
            get
            {
                string environmentName;
                #if DEBUG
                environmentName = "Development";
                #elif RELEASE
                environmentName = "Test";
                #endif
                return environmentName;
            }
        }

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "myconn": "connection"
  }
}

appsettings.Development.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "myconn": "connectionDev"
  }
}
{
  "ConnectionStrings": {
    "myconn": "connectionDev"
  }
}

launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49416/",
      "sslPort": 44368
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express Test": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Test"
      }
    },
    "WICAPI": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}
2

There are 2 answers

0
Carlos E. Carbonell On

ASP.NET Core actually does that for you. There's no need to do anything. Leave Program.cs as default:

        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

The general configuration must be on appsettings.json and just write the configs you want to override from appsettings.json in your appsettings.{env}.json file, being {env} the name of the environment, eg:

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "myconn": "connection"
  }
}

appsettings.Development.json

{
  "ConnectionStrings": {
    "myconn": "connectionDev"
  }
}
3
cuong nguyen manh On
  • Firstly you need to create launchSettings.json in Properties

enter image description here

With content some thing like this. Update follow your business.

    {
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "http://localhost/test",
      "sslPort": 0
    },
    "iisExpress": {
      "applicationUrl": "http://localhost:51572/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "test.Web.Local": {
      "commandName": "IIS",
      "launchBrowser": true,
      "launchUrl": "http://localhost/test",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:51572/"
    },
    "test.Web.Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:51572/"
    },
    "test.Web.RC": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "ReleaseCandidate"
      },
      "applicationUrl": "http://localhost:51572/"
    },
    "test.Web.Prod": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      },
      "applicationUrl": "http://localhost:51572/"
    }
  }
}
  • Your json file

enter image description here

  • In startup file you can use

    services.Configure(Configuration.GetSection("ApplicationSettings"));

  • When run debug choose env you want run

Hope to help you.