I have an ASP.NET web application and I have some code that I want to execute only in the debug version. How to do this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
#if DEBUG your code #endif
You could also add ConditionalAttribute to method that is to be executed only when you build it in debug mode:
[Conditional("DEBUG")]
void SomeMethod()
{
}
Method 2
if (HttpContext.Current.IsDebuggingEnabled)
{
// this is executed only in the debug version
}
From MSDN:
HttpContext.IsDebuggingEnabled Property
Gets a value indicating whether the current HTTP request is in debug mode.
Method 3
I declared a property in my base page, or you can declare it in any static class you have in applicaition:
public static bool IsDebug
{
get
{
bool debug = false;
#if DEBUG
debug = true;
#endif
return debug;
}
}
Then to achieve your desire do:
if (IsDebug)
{
//Your code
}
else
{
//not debug mode
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0