- ASP.NET Core reads the environment variable
ASPNETCORE_ENVIRONMENT
at application startup and stores that value in IHostingEnvironment.EnvironmentName
.
ASPNETCORE_ENVIRONMENT
can be set to any value, but three values are supported by the framework: Development, Staging, and Production.
- If
ASPNETCORE_ENVIRONMENT
isn't set, it will default to Production.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
// Handle development settings.
}
if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
{
// Handle other environments settings.
}
}
- On Windows and macOS, environment variables and values are not case sensitive.
- Linux environment variables and values are case sensitive by default.
- The environment for local machine development can be set in the
*Properties\\launchSettings.json*
file of the project.
- Environment values set in launchSettings.json override values set in the system environment.
- The following JSON shows three profiles from a launchSettings.json file:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "<http://localhost:54339/>",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApp1": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
},
"applicationUrl": "<http://localhost:54340/>"
},
"Kestrel Staging": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_My_Environment": "1",
"ASPNETCORE_DETAILEDERRORS": "1",
"ASPNETCORE_ENVIRONMENT": "Staging"
},
"applicationUrl": "<http://localhost:51997/>"
}
}
}
- When the application is launched with dotnet run, the first profile with
"commandName": "Project"
will be used.
- The value of
commandName
specifies the web server to launch. commandName
can be one of : IIS Express, IIS, and Project (which launches Kestrel).
- When an app is launched with dotnet run:
- launchSettings.json is read if available
environmentVariables
settings in launchSettings.json override environment variables.
- The hosting environment is displayed.
Setting the Environment
- If the environment isn't set, it will default to
Production
which disables most debugging features.
- The method for setting the environment depends on the operating system.
Azure App Services
- Select the Application settings blade.
- Add the key and value in App settings.
Windows
- To set the
ASPNETCORE_ENVIRONMENT
for the current session, if the app is started using dotnet run
, the following commands are used
Command line:
set ASPNETCORE_E NVIRONMENT=Development
PowerShell:
$Env:ASPNETCORE_ENVIRONMENT = "Development"