Skip to content

ASPNET Hosting Environments — Managing Environments in ASP.NET Core

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you'll learn about ASPNET Hosting Environments. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Hosting environments in ASP.NET Core enable environment-specific configuration, middleware, and behavior without conditional compilation.

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI();
}
else
{
    app.UseExceptionHandler("/error");
    app.UseHsts();
}

if (app.Environment.IsStaging())
{
    app.UseMiddleware<RequestLoggingMiddleware>();
}

// Launch settings
// "profiles": { "Development": { "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }

Environment-based configuration prevents accidental use of development or staging resources in production.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro