Cheat Sheet - Section 13 - ASP - Net Core
Cheat Sheet - Section 13 - ASP - Net Core
Introduction to Environments
Development
The environment, where the developer makes changes in the code, commits code to the source
control.
Staging
The environment, where the application runs on a server, from which other developers and quality
controllers access the application.
Production
Environment Setting
in launchSettings.json
"profiles":
"profileName":
"environmentVariables":
"DOTNET_ENVIRONMENT": "EnvironmentNameHere",
"ASPNETCORE_ENVIRONMENT": "EnvironmentNameHere"
}
app.Environment
IWebHostEnvironment
EnvironmentName
ContentRootPath
IsDevelopment()
IsStaging()
IsProduction()
IsEnvironment(string environmentName)
Returns Boolean true, if the current environment name matches with the specified environment.
Access Environment in Controller and other classes
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
_webHost = webHost;
in Program.cs
if (app.Environment.IsDevelopment()
app.UseDeveloperExceptionPage();
Process-Level Environment
The environment variables are stored & accessible within the same process only.
Setting Environment Variables in Process
$Env:Environment="EnvironmentName"
include
<environment include="Environment1,Environment2">
</environment>
It renders the content only when the current environment name matches with either of the
specified environment names in the "include" property.
exclude
<environment exclude="Environment1,Environment2">
</environment>
It renders the content only when the current environment name doesn't match with either of the
specified environment names in the "exclude" property.