Skip to content

Conversation

guardrex
Copy link
Collaborator

@guardrex guardrex commented Aug 29, 2025

Fixes #35807
Fixes #31962

  • The outline is below. I figure going with the following general order: environments (named), control code, and then setting the environment. If you want to change the order a great deal, I recommend giving me a new outline in a comment.
  • The (crummy IMO 😆) samples were placed into the samples repo on Add Environments sample apps AspNetCore.Docs.Samples#283 at https://github.com/dotnet/AspNetCore.Docs.Samples/tree/main/fundamentals/environments. I'll 🔪 💀 them here at the end because I don't want the diff overwhelmed with file changes during review.
  • This one doesn't adopt the dreaded INCLUDES files, but it did copy the entire article at .NET 6, which led (again 😩) to the poor update practice of only updating the current release content. The Azure Apps guidance wasn't updated, so looking at that with the <6.0 content and trying to use the portal would lead to an awfully 😕😖😠 dev. Besides, I'm 💀 that bit anyway in favor of a link to Azure docs to harden that section against future portal changes.
  • Per reader request, this adds Docker guidance for setting the environment.
  • WRT ✨ Startup class magic ✨ ... It has fallen out of favor in templates, but that's all fully supported AFAIK today and for the future ... and still could be rather helpful if an app has a lot of custom environment code deltas to deal with. Therefore, I'm resurfacing it in the latest guidance. It's at the very bottom of the article, so it's out of the way. My guess is that some devs would still like to use these approaches. I'm 👂 for feedback on that direction. Do you want me to go back to versioning it out at .NET 6?
  • The Blazor article (this link opens the 10.0 version because we have several section changes for 10.0+) is specifically focused on Blazor scenarios, but I'm still pitching consideration on (eventually) moving Blazor fundamentals (and host/deploy and security) coverage to the main doc set on the grounds that MVC and RP nodes never had their own split coverage for "fundamentals" subjects.

The initial outline for the article on the PR ...

  • Introductory remarks
  • Environments
  • Logging
  • Environment variables that determine the runtime environment
  • Control code execution by environment
  • Control rendered content in Razor Pages and MVC
  • Set the environment in a command shell when the app is run (dotnet run)
  • Set the environment with the launch settings file (launchSettings.json)
  • Set the environment with an environment variable
    • Azure App Service
    • Windows
      • Set environment variable for a process
      • Set environment variable globally
      • Set the environment for apps deployed to IIS
    • macOS and Linux
    • Docker
      • Use a Dockerfile
      • Use Docker Compose
      • Use the docker run command
      • Docker environment file
  • Set the environment in the app's startup code
  • Load configuration by environment
  • Environment-based Startup class and methods
    • Inject IWebHostEnvironment into the Startup class
    • Startup class conventions
    • Startup class method conventions
  • Additional resources

Internal previews

📄 File 🔗 Preview link
aspnetcore/fundamentals/environments.md aspnetcore/fundamentals/environments

@guardrex guardrex self-assigned this Aug 29, 2025
@guardrex guardrex marked this pull request as draft August 29, 2025 15:45
@guardrex guardrex requested a review from Copilot September 3, 2025 15:10
@guardrex guardrex marked this pull request as ready for review September 3, 2025 15:16
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR completely overhauls the ASP.NET Core environments documentation article to improve its organization, add new content for Docker scenarios, and modernize the guidance. The update addresses two GitHub issues (#35807 and #31962) by restructuring the content with a more logical flow and adding comprehensive Docker environment configuration guidance.

Key changes include:

  • Restructured the article with a clearer outline focusing on environments, control mechanisms, and setting approaches
  • Added comprehensive Docker guidance (Dockerfile, Docker Compose, docker run, environment files)
  • Consolidated versioned content while maintaining support for Startup class patterns
  • Updated Azure App Service guidance to reference external documentation

@guardrex guardrex requested a review from danroth27 September 3, 2025 15:31
@guardrex guardrex closed this Sep 3, 2025
@guardrex guardrex reopened this Sep 3, 2025

:::moniker-end
The Production environment is configured to maximize security, performance, and app reliability. Some common settings that differ from the Development environment include:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This revision makes it sound like the framework handles these production configuration changes, but I think most of them are up to the app developer to handle. Can we make it clearer that this is an example list of production concerns that the developer may want to address, not a list of things the framework automatically does?

Comment on lines -160 to +211
* Client-side resources are bundled, minified, and potentially served from a CDN.
* Diagnostic error pages disabled.
* Friendly error pages enabled.
* Production [logging](xref:fundamentals/logging/index) and monitoring enabled. For example, using [Application Insights](/azure/application-insights/app-insights-asp-net-core).
When using [Visual Studio Code](https://code.visualstudio.com/), set the `ASPNETCORE_ENVIRONMENT` environment variable in the `.vscode/launch.json` in the `env` section, along with any other environment variables set in the section:

## Set the environment by setting an environment variable
```json
"env": {
"ASPNETCORE_ENVIRONMENT": "Staging",
...
},
```

It's often useful to set a specific environment for testing with an environment variable or platform setting. If the environment isn't set, it defaults to `Production`, which disables most debugging features. The method for setting the environment depends on the operating system.
The `.vscode/launch.json` file is only used by Visual Studio Code.
Copy link
Member

@danroth27 danroth27 Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using launch.json isn't necessary with C# Dev Kit. C# Dev Kit in Visual Studio Code will pick up the available launch profiles from your launchSettings.json file.


When the host is built, the last environment setting read by the app determines the app's environment. The app's environment can't be changed while the app is running.
## Set the environment with an environment variable
Copy link
Member

@danroth27 danroth27 Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think we need to be documenting how to set an environment variable on various platforms, like Windows, macOS, and Linux. I'm ok keeping some pointers on how to do it for App Service, IIS, Docker, and dotnet run, although I'd prefer those pointers to mostly be links to the docs for the corresponding service to tech.

Copy link
Member

@danroth27 danroth27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start! Some feedback.

@guardrex
Copy link
Collaborator Author

@danroth27 ... Thanks! I'll get the article updated in the morning and ping u. Then, Javier after you, followed by Tom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update the sample app for "Use multiple environments in ASP.NET Core" to be based on Blazor instead of Razor Pages Add an example of Docker File
2 participants