Author : MD TAREQ HASSAN
Details: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
ASPNETCORE_ENVIRONMENT variable
ASPNETCORE_ENVIRONMENTis an environment variable that can be set in project Properties &launchSettings.json- ASP.NET Core reads
ASPNETCORE_ENVIRONMENTat app startup (and stores the value inIHostingEnvironment.EnvironmentName) - you can set
ASPNETCORE_ENVIRONMENTto any value - if
ASPNETCORE_ENVIRONMENTisn’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.jsonoverrides value ofASPNETCORE_ENVIRONMENTfrom 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.jsonwill 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");
}
// ... ... ...
}