ASP.NET Core 中的環境標籤協助程式Environment Tag Helper in ASP.NET Core

作者:Peter KellnerHisham Bin AteyaLuke LathamBy Peter Kellner, Hisham Bin Ateya, and Luke Latham

環境標籤協助程式依據目前的主控環境,有條件地轉譯含括內容。The Environment Tag Helper conditionally renders its enclosed content based on the current hosting environment. 環境標籤協助程式的單一屬性 names,是以逗號分隔的環境名稱清單。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.

如需標籤協助程式的概觀,請參閱 ASP.NET Core 中的標籤協助程式For an overview of Tag Helpers, see ASP.NET Core 中的標籤協助程式.

環境標籤協助程式屬性Environment Tag Helper Attributes

名稱names

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.

環境值會與 IHostingEnvironment.EnvironmentName 所傳回的目前值比較。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 和 exclude 屬性include and exclude attributes

include & exclude 屬性會依據包含或排除的主控環境名稱,來控制含括內容的轉譯。include & exclude attributes control rendering the enclosed content based on the included or excluded hosting environment names.

includeinclude

include 屬性會表現出類似 names 屬性的行為。The include property exhibits similar behavior to the names attribute. include 屬性值中列出的環境,必須與應用程式的主控環境 (IHostingEnvironment.EnvironmentName) 相符,才能轉譯 <environment> 標籤的內容。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>

excludeexclude

include 屬性相反,當主控環境與 exclude 屬性值中列出的環境不相符時,將轉譯 <environment> 標記的內容。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