Author : MD TAREQ HASSAN
Details: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
ASPNETCORE_ENVIRONMENT variable
ASPNETCORE_ENVIRONMENT
is an environment variable that can be set in project Properties &launchSettings.json
- ASP.NET Core reads
ASPNETCORE_ENVIRONMENT
at app startup (and stores the value inIHostingEnvironment.EnvironmentName
) - you can set
ASPNETCORE_ENVIRONMENT
to any value - if
ASPNETCORE_ENVIRONMENT
isn’t set, it defaults to ‘Production’ - values provided by aspnet core: ‘Development’, ‘Production’ & ‘Staging’
Setting environment variable
1. Project properties
- right click on project > properties > Debug > Environment variables
- Name:
ASPNETCORE_ENVIRONMENT
, Value:Foo
2. launchSettings
Solution explorer > Show all files > Properties > launchSettings.json
"profiles": {
"IIS Express": {
// ... ... ...
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Notes:
launchSettings.json
overrides value ofASPNETCORE_ENVIRONMENT
from project properties- you must restart VS after changing
launchSettings.json
Multiple appsettings json
- default:
appsettings.json
- depending on the value of ASPNETCORE_ENVIRONMENT,
appsettings.X.json
will be used
appsettings for corresponding environment
ASPNETCORE_ENVIRONMENT | appsettings |
---|---|
not set | appsettings.json |
Development | appsettings.Development.json |
Production | appsettings.Production.json |
Foo | appsettings.Foo.json |
ExceptionHandler depending on environment
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
{
app.UseExceptionHandler("/Error");
}
// ... ... ...
}