Environment Tag Helper in ASP.NET Core

By Peter Kellner, Hisham Bin Ateya, and Luke Latham

The Environment Tag Helper conditionally renders its enclosed content based on the current hosting environment. The Environment Tag Helper's single attribute, names, is a comma-separated list of environment names. If any of the provided environment names match the current environment, the enclosed content is rendered.

For an overview of Tag Helpers, see Tag Helpers in ASP.NET Core.

Environment Tag Helper Attributes

names

names accepts a single hosting environment name or a comma-separated list of hosting environment names that trigger the rendering of the enclosed content.

Environment values are compared to the current value returned by IHostingEnvironment.EnvironmentName. The comparison ignores case.

The following example uses an Environment Tag Helper. The content is rendered if the hosting environment is Staging or Production:

<environment names="Staging,Production">
    <strong>HostingEnvironment.EnvironmentName is Staging or Production</strong>
</environment>

include and exclude attributes

include & exclude attributes control rendering the enclosed content based on the included or excluded hosting environment names.

include

The include property exhibits similar behavior to the names attribute. An environment listed in the include attribute value must match the app's hosting environment (IHostingEnvironment.EnvironmentName) to render the content of the <environment> tag.

<environment include="Staging,Production">
    <strong>HostingEnvironment.EnvironmentName is Staging or Production</strong>
</environment>

exclude

In contrast to the include attribute, the content of the <environment> tag is rendered when the hosting environment doesn't match an environment listed in the exclude attribute value.

<environment exclude="Development">
    <strong>HostingEnvironment.EnvironmentName is not Development</strong>
</environment>

Additional resources