Detecting Debug mode in ASP.NET Core / .NET 5

I was working on a caching framework in ASP.NET Core and one thing I needed to know whether the application is running in debug mode.

In the regular .NET framework, this was available through the HttpContext, as per the following syntax:

HttpContext.Current.IsDebuggingEnabled

In ASP.NET Core, this property is not available anymore and the HttpContext is now accessible from the Http namespace: Microsoft.AspNetCore.Http.HttpContext

Therefore, the usual way to check if the ASP.NET Core application is in debug mode is to check Debugger.IsAttached as per the following syntax:

if(Debugger.IsAttached) {
//Your code here.
}

However, this property is true only when the application is attached to the debugging process. If you want to indicate that the application is running in development/debug mode and not on production server, you can add a key to the appsettings.json as per the following screenshot:


Then in your C# code you can get the value from the configuration:

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website