Determine ASP.NET Core environment name in the views

24,446

Solution 1

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

Solution 2

I just made a simple API controller:

[Route("api/[controller]")]
public class DebugController : Controller
{
    private IHostingEnvironment _hostingEnv;

    public DebugController(IHostingEnvironment hostingEnv)
    {
        _hostingEnv = hostingEnv;
    }

    [HttpGet("environment")]
    public IActionResult Environment()
    {
        return Ok(_hostingEnv.EnvironmentName);
    }

Then I just run /api/debug/environment to see the value.

Solution 3

The following works In .net core 2.2:

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment env
@using Microsoft.AspNetCore.Hosting 

if (env.IsProduction())
{
   //You can also use:

   //env.IsStaging();
   //env.IsDevelopment();
   //env.IsEnvironment("EnvironmentName");
}

Solution 4

Starting from .NET Core 3.1 it is recommended to use

@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnv

Add this to at the top of your Razor view. You can use @hostingEnv.EnvironmentName to get the current environment.

Share:
24,446
Nikolay Kostov
Author by

Nikolay Kostov

Blog: https://nikolay.it GitHub: https://github.com/NikolayIT CV: https://nikolay.it/Pages/About-me

Updated on July 05, 2022

Comments

  • Nikolay Kostov
    Nikolay Kostov almost 2 years

    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: @......... -->
    
  • aruno
    aruno about 6 years
    (this doesn't help with the views, but it's the quickest way I know of to check if my config is correct)
  • Nikolay Kostov
    Nikolay Kostov about 5 years
    You don't need @using Microsoft.AspNetCore.Hosting. Also your answer is the same as the one selected as best answer.
  • Shadi Alnamrouti
    Shadi Alnamrouti about 5 years
    @NikolayKostov, you're wrong. To use the extension methods .IsProduction() and similar, then you must use using Microsoft.AspNetCore.Hosting. My answer is different because the best answer is no longer valid in .net core 2.2. I suggest you test the best answer and my answer under .net core 2.2 before you say anything.
  • solublefish
    solublefish over 4 years
    Apparently in ASP.NET Core 3, it's @inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnv instead
  • Michael Freidgeim
    Michael Freidgeim about 3 years
    The answer belongs to the separate question stackoverflow.com/questions/56495475/…