Permalink
65 lines (43 sloc) 2.65 KB
title author description ms.author ms.custom ms.date uid
Environment Tag Helper in ASP.NET Core
pkellner
ASP.NET Core Environment Tag Helper defined including all properties
riande
mvc
10/10/2018
mvc/views/tag-helpers/builtin-th/environment-tag-helper

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 xref:mvc/views/tag-helpers/intro.

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>

::: moniker range=">= aspnetcore-2.0"

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>

::: moniker-end

Additional resources

  • xref:fundamentals/environments