How to execute code only in debug mode in ASP.NET How to execute code only in debug mode in ASP.NET asp.net asp.net

How to execute code only in debug mode in ASP.NET


#if DEBUGyour 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    }