To configure and manage multiple environments in ASP.NET Core, you use a combination of environment variables
and structured configuration files. At a high level, you create separate appsettings.{EnvironmentName}.json files
and set the ASPNETCORE_ENVIRONMENT variable to tell the application which file to use.
Step 1: Create Environment-Specific Configuration Files
By default, an ASP.NET Core project includes appsettings.json and appsettings.Development.json.
You can create additional files for other environments, such as staging or production.
Settings in environment-specific files override the base appsettings.json file.
Example File Structure:
appsettings.json(base settings for all environments)appsettings.Development.json(overrides for the Development environment)appsettings.Staging.json(overrides for the Staging environment)appsettings.Production.json(overrides for the Production environment)
Example appsettings.Development.json:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DevelopmentDb;..."
}
}
Step 2: Set the ASPNETCORE_ENVIRONMENT Variable
ASP.NET Core applications read the ASPNETCORE_ENVIRONMENT variable at startup to determine the current environment.
To set the variable:
-
In Visual Studio: Modify the
launchSettings.jsonfile inside thePropertiesfolder. This is used only for local development and overrides any system-level environment variables.
"profiles": {
"MyProject": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"StagingProfile": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
}
}
- On the server: Set the
ASPNETCORE_ENVIRONMENTvariable in your hosting environment (IIS, Azure, Docker). - IIS: Configure it in the
web.configfile or in IIS Manager for the app pool. - Docker: Set it in your
Dockerfileordocker-compose.yml. - Azure App Service: Add a new Application Setting named
ASPNETCORE_ENVIRONMENTin the Azure portal.
Step 3: Access Configuration in Your Code
ASP.NET Core's built-in dependency injection provides the IConfiguration and IHostEnvironment services
to access configuration settings and determine the current environment at runtime.
Using IConfiguration to Retrieve Settings:
// In a controller or service, inject IConfiguration
public class MyService(IConfiguration configuration)
{
private readonly string _connectionString = configuration.GetConnectionString("DefaultConnection");
public void DoSomething()
{
// Use the connection string dynamically based on the current environment
}
}
Using IHostEnvironment for Environment-Specific Logic:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage(); // Show detailed errors in development
}
else
{
app.UseExceptionHandler("/Error"); // Generic error page in production
}
if (env.IsEnvironment("Staging"))
{
// Add staging-specific middleware
}
}
Advanced Environment Management
- Environment-specific service registration: Conditionally register services using
if (env.IsDevelopment())insideConfigureServices. - Centralized configuration: Use Azure App Configuration or HashiCorp Vault to manage and retrieve settings dynamically for multiple applications.
- Configuration key hierarchy: Environment variables take precedence over JSON settings, and environment-specific JSON overrides the base
appsettings.json.