liveBook · Manning

ASP.NET Core environment

As explained in Building Web APIs with ASP.NET Core

ASPNETCORE_ENVIRONMENT is an environment variable that specifies the runtime environment in which an ASP.NET Core application runs.

An ASP.NET Core environment identifies the phase in which an application is running. Common values are Development for debugging, Staging for testing, and Production for end users. Set the runtime environment with DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT in the application's execution context. section 2.4.2

For a local web API, set ASPNETCORE_ENVIRONMENT to Development under each launch profile's environmentVariables entry in launchSettings.json. This lets the IIS Express and Kestrel profiles start the application in the intended environment. section 2.4.1

The environment also selects configuration values. appsettings.json supplies settings shared across environments; appsettings.Development.json adds development-only settings and replaces matching values from the shared file because it is read afterward. section 2.4.2

Check environment-dependent behavior

In Program.cs, app.Environment.IsDevelopment() can enable Swagger UI and DeveloperExceptionPageMiddleware for Development while the other branch uses ExceptionHandlerMiddleware. Add a test endpoint that throws an exception, start the API, and visit /error/test: Development displays the developer exception page. Change ASPNETCORE_ENVIRONMENT to Production to test the exception handler instead. A separate UseDeveloperExceptionPage configuration key can control that choice without changing the environment. section 2.4.3 section 2.4.2

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
 
if (app.Configuration.GetValue<bool>("UseDeveloperExceptionPage"))   #1
    app.UseDeveloperExceptionPage();                                 #2
else
    app.UseExceptionHandler("/error");                                #3

Compare the environment check for Swagger with the separate configuration-key check that chooses exception middleware. section 2.4.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through local setup, configuration overrides, and middleware testing; defers production-server setup.
Examples
Uses Development launch profiles and a throwing /error/test endpoint to compare developer and production-style error responses.
What it assumes
Introduces runtime environments from zero while working with a generated ASP.NET Core web API and its configuration files.
What you can do after
Operate/configure it by setting a local launch profile, overriding development settings, and verifying the selected exception handler.
How it connects
Connects launchSettings.json, appsettings.json, Program.cs, Swagger UI, and exception-handling middleware.

Also explained in

Compare how 2 books explain ASP.NET Core environment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.