john Cogdle
john Cogdle

Reputation: 1533

Environment Tag Helper in .NET Core

I just create a .net core 2.0 project on my visual studio and found in _Layout.cshtml few new properties like called- "environment". I worked on MVC5 but there was no such properties. What these propertie does? Is it replacement of Rezor Syntax which i used in MVC5 view? Please provide details with doc for use those properties to get started with those.

_Layout.cshtml:

 <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/css/site.css" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
    </environment>

pic

Upvotes: 8

Views: 10186

Answers (3)

Yashar Azadvatan
Yashar Azadvatan

Reputation: 159

We have 3 Environments; Development, Staging and Production.

This tag helper help us to render what we need in different environments.

the code below means that, if we are in DEVELOPMENT environment, render this css files.

<environment include="Development">
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</environment>

and the code below means that render the content when we are NOT in DEVELOPMENT environment.

<environment exclude="Development">
    <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
          asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
          asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
    <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>

*note: asp-fallback-href in code above means if can't connect to cdn, go and use minified bootstrap file from server!

also you can write code above like this:

<environment include="Staging, Production">
    <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
          asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
          asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
    <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>

that means render the content if we are in STAGING or PRODUCTION environments.

Upvotes: 11

Jordan Bowker
Jordan Bowker

Reputation: 276

The environment tag helper uses the value of IHostingEnvironment.EnvironmentName to include/exclude content in the DOM (Document Object Model) based on the current application environment.

In your snippet, the first two style sheets are included when running your code in a development environment, and the bottom two are excluded when in development environment.

See Microsoft docs on Tag Helper in ASP.NET Core and Environment Tag Helper , which could help more clearly to answer your question.

Upvotes: 7

k1dev
k1dev

Reputation: 641

The Environment Tag Helper conditionally renders its enclosed content based on the current hosting environment.

The include attribute basically means render the code whether you're in Development environment, while it won't be rendered with exclude attribute.

Upvotes: 0

Related Questions