ASP.Net Core,在控制器中检测调试与不调试 [英] ASP.Net Core, detecting debugging vs. not debugging in a controller

查看:107
本文介绍了ASP.Net Core,在控制器中检测调试与不调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个ASP.Net代码Web应用程序,并且我想在控制器中使用if语句检查我是否处于调试模式.我知道在Startup.cs文件中可以检查env.IsDevelopment(),但这是因为IHostingEnvironment被传递到了其中.我无法找到一种在常规控制器中检查此状态的方法.在ASP.Net Core中,有没有一种方法可以检测到我在缺少的控制器中处于调试模式的时间?

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?

推荐答案

您应该能够将IHostingEnvironment注入到控制器构造函数中.

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();
}

更新: IHostingEnvironment在.Net Core 3.1中已过时,请参阅.Net Core 3.1+的以下内容 https://stackoverflow.com/a/61703339/2525561

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

这篇关于ASP.Net Core,在控制器中检测调试与不调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆