8

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

2
  • 1
    It's still Razor, it's just not using the same HTML helpers. It's using a new concept called tag helpers.
    – mason
    Jun 13, 2018 at 20:29
  • Please change the word "property" to "element", otherwise it's a good question. Jun 13, 2018 at 22:58

3 Answers 3

11

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.

1
  • 1
    This is the best answer from all as you described "Staging and Production" environment too. Aug 27, 2019 at 4:50
7

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.

1
  • 2
    Link only answers are discouraged on Stack Overflow. Links are fine as a supplement, but your answer should be able to stand on its own merit without the link. Please summary the necessary information here.
    – mason
    Jun 13, 2018 at 20:21
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.