开发者

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

I have an ASP.NET w开发者_StackOverflow中文版eb application and I have some code that I want to execute only in the debug version. How to do this?


#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
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜