ASP.Net Core,在控制器中检测调试与不调试 [英] ASP.Net Core, detecting debugging vs. not debugging in a controller
问题描述
我正在编写我的第一个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屋!