"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Does ASP.NET Core Automatically Load Configuration Settings Based on Different Build Environments?

How Does ASP.NET Core Automatically Load Configuration Settings Based on Different Build Environments?

Posted on 2025-03-24
Browse:653

How Does ASP.NET Core Automatically Load Configuration Settings Based on Different Build Environments?

ASP.NET Core application environment configuration automatically loads

ASP.NET Core applications use appsettings.json file storage configuration settings, including database connection strings and API URLs, etc. However, these settings usually vary by development environment (local, test, production). To solve this problem, ASP.NET Core provides a flexible mechanism to load different appsettings files based on the build configuration.

Multiple Appsettings files

The

solution involves creating multiple appsettings files such as appsettings.Production.json and appsettings.Development.json. Each file contains configuration settings specific to the corresponding environment.

Auto load

In order to automatically load the corresponding appsets file, you can use the Host.CreateDefaultBuilder method of ASP.NET Core. This method initializes the configuration object according to the following sources in the following order:

  • appsettings.json
  • ]
  • appsettings.{Environment}.json (for example appsettings.Development.json)
  • Application key (in the development environment)
  • Environment variables
  • Command line parameters

The configuration system will automatically load the corresponding appsettings.{Environment}.json environment variable to the desired environment (for example, "Production" or "Development"). Environment variable settings

The environment variable can be set in the following ways:

Visual Studio: Project > Properties > Debug > Environment Variables
  • Visual Studio Code: Edit
  • .vscode/launch.json
  • > env] Startup settings: Properties/launchSettings.json > environmentVariables
  • .NET CLI: Use syntax for setting environment variables that are suitable for your operating system
  • Code Example

The following is an example using

Host.CreateDefaultBuilder

:

WebHost.CreateDefaultBuilder(args) .UseStartup() .Build();
WebHost.CreateDefaultBuilder(args)
    .UseStartup()
    .Build();
Startup

class, the configuration object will be automatically injected:

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } }
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
}
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3