Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine ASP.NET Core environment name in the views

The new ASP.NET Core framework gives us ability to execute different html for different environments:

<environment names="Development">     <link rel="stylesheet" href="~/lib/material-design-lite/material.css" />     <link rel="stylesheet" href="~/css/site.css" /> </environment> <environment names="Staging,Production">     <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.0.0/css/bootstrap.min.css"           asp-fallback-href="~/lib/material-design-lite/material.min.css"           asp-fallback-test-class="hidden" asp-fallback-test-property="visibility" asp-fallback-test-value="hidden"/>     <link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/> </environment> 

But how can I determine and visualize the name of the current environment in the _Layout.cshtml of an ASP.NET Core MVC web application?

For example I want to visualize the environment name (Production, Staging, Dev) as a HTML comment for debugging purposes:

<!-- Environment name: @......... --> 
like image 390
Nikolay Kostov Avatar asked Nov 15 '15 12:11

Nikolay Kostov


People also ask

How does .NET Core determine environment?

ASP.NET Core uses the ASPNETCORE_ENVIRONMENT environment variable to determine the current environment. By default, if you run your application without setting this value, it will automatically default to the Production environment.

What are environment variables in ASP.NET Core?

. NET core uses environment variables to indicate in which environment the application is running and to allow the app to be configured appropriately. They provide a static class Environment in the system namespace to access the environment variables.

What is ASP.NET host environment?

The hosting environment in ASP.NET Core is used to indicate at runtime on which environment (Development, Staging, or Production) an ASP.NET Core application is running.


1 Answers

You can inject the service IHostingEnvironment in your view by doing
@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv
and do a @hostingEnv.EnvironmentName

like image 147
Kiran Avatar answered Sep 21 '22 01:09

Kiran