Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now

ASP.NET Core Environments

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:01:00

Environments

Environments in ASP.NET Core allow you to manage and customize your application's behavior for different runtime scenarios, such as Development, Staging, and Production.

Set the environment by setting an environment variable

You can set the environment for your ASP.NET Core application using an environment variable like this:

Example

export ASPNETCORE_ENVIRONMENT=Production

Set the environment in the code

You can also set the environment programmatically in your code. For example, in your Program.cs file:

Example

public static IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
      webBuilder.UseStartup<Startup>();
    })
    .UseEnvironment("Staging"); // Set the environment here

Configuration by environment

You can configure your application differently based on the environment. In your Startup.cs or appsettings.json, you can define settings specific to each environment:

Example

if (env.IsDevelopment())
{
  // Development-specific configuration
}
else if (env.IsStaging())
{
  // Staging-specific configuration
}
else if (env.IsProduction())
{
  // Production-specific configuration
}

Configure services and middleware by environment

You can also configure services and middleware based on the environment. For example, conditionally add middleware or services in your Startup.cs:

Example

if (env.IsDevelopment())
{
  app.UseDeveloperExceptionPage();
}
else
{
  app.UseExceptionHandler("/Home/Error");
  app.UseHsts();
}
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this