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?
You can do this by clicking on the arrow next to Run button in Visual Studio and selecting 'Project Debug Properties'. From there, you can go to 'Application Arguments' and enter the arguments you want.
You can also click on the line you want to skip to and hit Ctrl+F10 (Run to Cursor). It will jump directly to that line.
In the Web. config file, locate the compilation element. Debugging is enabled when the debug attribute in the compilation element is set to true. Change the debug attribute to false to disable debugging for that application.
#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()
{
}
Detecting ASP.NET Debug mode
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With