Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
ASP.Net Core, detecting debugging vs. not debugging in a controller

I am writing my first ASP.Net code web app and in my controller I would like to have an if statement that checks to see if I am in debugging mode or not. I know in the Startup.cs file I can check env.IsDevelopment() but that is because the IHostingEnvironment is passed into it. I have not been able to find a way to check for this status inside a normal controller. Is there a way in ASP.Net Core to detect when I am in debug mode inside the controller that I am missing?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Update: @Pradeep Kumar's post below is the more correct answer here. This answer only indicates how to access the IsDevelopment() environment flag via dependency injection in a controller.

Update: IHostingEnvironment is obsolete in .Net Core 3.1 see the following for .Net Core 3.1+ https://stackoverflow.com/a/61703339/2525561

You should be able to just inject IHostingEnvironment into your controller constructor.

protected readonly IHostingEnvironment HostingEnvironment;

public TestController(IConfiguration configuration, IHostingEnvironment hostingEnv){
    this.Configuration = configuration;
    this.HostingEnvironment = hostingEnv;
}

[HttpGet]
public IActionResult Test(){
    if(this.HostingEnvironment.IsDevelopment()){
        // Do something
    }

    return View();
}
about 4 years ago · Santiago Trujillo Relatório

0

IHostingEnvironment lets you know the environment in which the application is running. Looks like what you need is the build configuration used to build the application i.e Debug/Release. In an ASP.NET Core web application, In order to get this information at compile time, there is no straight forward way, however you can have a property with conditional compilation using compiler directives, something like

public static bool IsDebug
{
  get
     {
      bool isDebug = false;
    #if DEBUG
       isDebug = true;
    #endif
       return isDebug;
      }
}

At runtime, you can check the value of IsDebug property to determine the build configuration. I would suggest to add this property to a common static or utility class which can be accessible from all your controllers.

about 4 years ago · Santiago Trujillo Relatório

0

It's not IHostingEnvironment nowadays, it's IWebHostingEnvironment. In ASP.NET Core 3.1, IHostingEnvironment causes a warning

CS0618  'IHostingEnvironment' is obsolete: 'This type is obsolete 
and will be removed in a future version. 
The recommended alternative is 
Microsoft.AspNetCore.Hosting.IWebHostEnvironment.'

Consequently, the accepted answer should be updated as follows:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;


public class TestController : Controller
{
    protected readonly IWebHostEnvironment HostEnvironment;

    public TestController(IWebHostEnvironment hostEnv) {
        this.HostEnvironment = hostEnv;
    }

    [HttpGet]
    public IActionResult Test(){
        if (this.HostEnvironment.IsDevelopment()){
            // Do something
        }

        return View();
    }
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda