-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add a built-in EnvironmentBoundary component for Blazor #64616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
1bb245b
b1df880
73d6e7d
0b21f28
4b1e0c7
d2cbc23
8eda182
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,143 @@ | ||||||||||||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||||||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||||||||||||
|
|
||||||||||||||||||
| using Microsoft.AspNetCore.Components.Rendering; | ||||||||||||||||||
| using Microsoft.Extensions.Hosting; | ||||||||||||||||||
|
|
||||||||||||||||||
| namespace Microsoft.AspNetCore.Components.Web; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <summary> | ||||||||||||||||||
| /// A component that renders its child content only when the current hosting environment | ||||||||||||||||||
| /// matches one of the specified environment names. | ||||||||||||||||||
| /// </summary> | ||||||||||||||||||
| /// <remarks> | ||||||||||||||||||
| /// <para> | ||||||||||||||||||
| /// This component is similar to the environment tag helper in MVC and Razor Pages. | ||||||||||||||||||
| /// </para> | ||||||||||||||||||
| /// <example> | ||||||||||||||||||
| /// The following example shows how to conditionally render content based on the environment: | ||||||||||||||||||
| /// <code> | ||||||||||||||||||
| /// <EnvironmentBoundary Include="Development"> | ||||||||||||||||||
| /// <div class="alert alert-warning"> | ||||||||||||||||||
| /// You are running in Development mode. Debug features are enabled. | ||||||||||||||||||
| /// </div> | ||||||||||||||||||
| /// </EnvironmentBoundary> | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// <EnvironmentBoundary Include="Development, Staging"> | ||||||||||||||||||
| /// <p>This is a pre-production environment.</p> | ||||||||||||||||||
| /// </EnvironmentBoundary> | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// <EnvironmentBoundary Exclude="Production"> | ||||||||||||||||||
| /// <p>Debug information: @DateTime.Now</p> | ||||||||||||||||||
| /// </EnvironmentBoundary> | ||||||||||||||||||
| /// </code> | ||||||||||||||||||
| /// </example> | ||||||||||||||||||
| /// </remarks> | ||||||||||||||||||
| public sealed class EnvironmentBoundary : ComponentBase | ||||||||||||||||||
| { | ||||||||||||||||||
| private static readonly char[] NameSeparator = [',']; | ||||||||||||||||||
|
|
||||||||||||||||||
| [Inject] | ||||||||||||||||||
| private IHostEnvironment HostEnvironment { get; set; } = default!; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <summary> | ||||||||||||||||||
| /// Gets or sets a comma-separated list of environment names in which the content should be rendered. | ||||||||||||||||||
| /// If the current environment is also in the <see cref="Exclude"/> list, the content will not be rendered. | ||||||||||||||||||
| /// </summary> | ||||||||||||||||||
| /// <remarks> | ||||||||||||||||||
| /// The specified environment names are compared case insensitively. | ||||||||||||||||||
| /// </remarks> | ||||||||||||||||||
| [Parameter] | ||||||||||||||||||
| public string? Include { get; set; } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <summary> | ||||||||||||||||||
| /// Gets or sets a comma-separated list of environment names in which the content will not be rendered. | ||||||||||||||||||
| /// </summary> | ||||||||||||||||||
| /// <remarks> | ||||||||||||||||||
| /// The specified environment names are compared case insensitively. | ||||||||||||||||||
| /// </remarks> | ||||||||||||||||||
| [Parameter] | ||||||||||||||||||
| public string? Exclude { get; set; } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <summary> | ||||||||||||||||||
| /// Gets or sets the content to be rendered when the environment matches. | ||||||||||||||||||
| /// </summary> | ||||||||||||||||||
| [Parameter] | ||||||||||||||||||
| public RenderFragment? ChildContent { get; set; } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||
| protected override void BuildRenderTree(RenderTreeBuilder builder) | ||||||||||||||||||
| { | ||||||||||||||||||
| if (ShouldRenderContent()) | ||||||||||||||||||
| { | ||||||||||||||||||
| builder.AddContent(0, ChildContent); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| private bool ShouldRenderContent() | ||||||||||||||||||
| { | ||||||||||||||||||
| var currentEnvironmentName = HostEnvironment.EnvironmentName?.Trim(); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (string.IsNullOrEmpty(currentEnvironmentName)) | ||||||||||||||||||
| { | ||||||||||||||||||
|
||||||||||||||||||
| { | |
| { | |
| // For consistency with MVC EnvironmentTagHelper, render content when environment name is not set | |
| // and no Include/Exclude are specified. | |
| if (string.IsNullOrWhiteSpace(Include) && string.IsNullOrWhiteSpace(Exclude)) | |
| { | |
| return true; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot This should live in the
Components.Webassembly, not in theComponentsassembly directly. Also, the name is not great. As an exception, we can name itEnvironmentBoundaryto begin with. We will decide on the final name later. As a result of this rename, there will be many unnecessary changes. Make sure you undo those. Also follow the guidance in src/Components/AGENTS.md to add an E2E test for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the component to
Components.Webassembly asEnvironmentBoundary, reverted unnecessarySystem.Environmentchanges, and added E2E test assets. Commit: 0b21f28