Environment variables in asp.net core | Asp.Net Core tutorial for beginners
Environment variables in asp.net core are used to work on different environments for that application. Here are common environments in any web application –
- Development
- QA (Test)
- Staging
- Production
Based on the nature of the application, client requirements, or other factors, we can add or remove the environments.
Environment variables help us (or the application) to choose the environment among all other environments.
We can create n number of environments in an asp.net core web application.
Benefits of having multiple environments in a web application
Case 1:
Situation: Suppose we are using multiple JS, CSS files in the web application. If we refer all these files one by one directly on any page of the application (let’s say _Layout.cshtml) then this will impact the performance of the application on production.
If we use the bundle and minification then it will be hard to debug the code for the developers.
Solution: We can fix this situation by using multiple environments. i.e. use individual files for the development environment and use the minified files for the production (non-development) environment.
Case 2:
By using multiple environments, we can keep the production data secure. Developers will work on the dev database means no impact on the production database and other production settings.
How to set the environment variable in asp.net core application
We can set the environment variables in launchSettings.json file.
Open the launchSettings.json file and navigate to profiles section.
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"LearnAspNetCore": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
Here, you can notice we have an object key environmentVariables
and it has another key with name ASPNETCORE_ENVIRONMENT
.
This ASPNETCORE_ENVIRONMENT
decides the current environment ion development machine.
The default value of ASPNETCORE_ENVIRONMENT
is Development
. It means the currently selected environment is Development
.
We can update the environment on the development machine by updating the value of
ASPNETCORE_ENVIRONMENT
property inlaunchSettings.json
file.
Here is an example of using Production environment in asp.net core
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
How to get the current environment in asp.net core application
We can get the current environment by using IWebHostEnvironment
in asp.net core application.
To view the live use of IWebHostEnvironment let’s open startup.cs
class and navigate to the Configure
method. Here you will see this piece of code and notice on env.IsDevelopment()
.
if (env.IsDevelopment())
{
// other code goes here
}
This IWebHostEnvironment has couple of methods and properties to help us find out the current environment.
IsDevelopment()
Returns true
if the current host environment name is Development
otherwise returns false
.
IsProduction()
Returns true
if the current host environment name is Production
otherwise returns false
.
IsStaging()
Returns true
if the current host environment name is Staging
otherwise returns false
.
IsEnvironment()
IsEnvironment() is used to compare the current host name with the specified value.
Let’s say the name of the environment is XYZ
. In this situation, to identify the current name we can use IsEnvironment() method.
bool isXYZEnvironment = env.IsEnvironment("XYZ");
EnvironmentName
EnvironmentName is a property and returns the current environment name.
string currentEnvironmetName = env.EnvironmentName;
// output
currentEnvironmetName = "Development";
Let’s assume, I want to display the message based on the environment. Here is the sample code that uses all the above methods and properties.
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
if (env.IsDevelopment())
{
await context.Response.WriteAsync("Development");
}
else if (env.IsStaging())
{
await context.Response.WriteAsync("Staging");
}
else if (env.IsProduction())
{
await context.Response.WriteAsync("Production");
}
else if (env.IsEnvironment("CustomEnvironment"))
{
await context.Response.WriteAsync("CustomEnvironment");
}
else
await context.Response.WriteAsync(env.EnvironmentName);
});
});