简体   繁体   中英

How to switch configuration while debugging .Net core 2.x app?

Have been looking for the right way to add configurations to my dot net core 2.0 web API.

Until now what I have done is:

  1. Added appsetteings.Development.json, appsetteings.Production.json

  2. In program.cs:

     public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureAppConfiguration((hostContext, config) => { var env = hostContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); }) .Build(); } 
  3. In startup.cs

     public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.Configure<dynamic>(Configuration); } 

The issue is whenever I debug my code it always takes settings from appsettings.Development.json .

I also found that the hostContext.HostingEnvironment.EnvironmentName always comes as development regardless of which environment I pick to debug in.

There is a setting with the project properties that is causing the Development environment setting to be used during debug.

Open Project properties Navigate to the Debug page In the Environment Variables section you will see:

ASPNETCORE_ENVIRONMENT | Development

在此处输入图片说明

If you remove this flag and debug you app it should be running without the Development settings.

The official documentation can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM