35

I am drawing a blank here for something that should be simple...

I am trying to do something like:

    <my:control runat="server" id="myid" Visible="<%= (is compilation debug mode?) %>" />
1
  • Not exactly what I was looking for (did not want to use code behind or directives), but I was able to use in the code behind, code from Nicholas Carey: DebugViewControl.Visible = ((System.Web.Configuration.CompilationSection)ConfigurationManager.GetSection(@"system.web/compilation")).Debug; Jun 1, 2011 at 17:35

5 Answers 5

60

The HttpContext.IsDebuggingEnabled property:

using System.Web;

if (HttpContext.Current.IsDebuggingEnabled) { /* ... */ }

From the documentation:

Gets a value indicating whether the current HTTP request is in debug mode[…] true if the request is in debug mode; otherwise, false.

2
54

This should get you the <compilation> element in the <system.web> section group:

using System.Web.Configuration ;

. . .

CompilationSection compilationSection = (CompilationSection)System.Configuration.ConfigurationManager.GetSection(@"system.web/compilation") ;

. . .

// check the DEBUG attribute on the <compilation> element
bool isDebugEnabled = compilationSection.Debug ;

Easy!

4
  • 1
    So easy, just sent it to a teammate who is trying to turn off his minify code in debug build. Thanks :)
    – Tien Do
    Jan 4, 2013 at 3:47
  • 11
    Why not HttpContext.Current.IsDebuggingEnabled ? Mar 25, 2013 at 13:01
  • 3
    @AdrianSalazar in some cases the HttpContext could be null, for exmaple a process running async background thread that are run separately from a context.
    – user623892
    May 27, 2013 at 15:16
  • 2
    @ErgonomicDeveloper well, please refer to the Question itself. We're rendering a control, not running async code. This thread will have an HttpContext, so I won't worry. May 28, 2013 at 12:54
15
<my:control runat="server" id="myid" Visible="<%= HttpContext.Current.IsDebuggingEnabled %>" />

See http://msdn.microsoft.com/en-us/library/system.web.httpcontext.isdebuggingenabled%28v=vs.90%29.aspx

or http://www.west-wind.com/weblog/posts/2007/Jan/19/Detecting-ASPNET-Debug-mode with a fruitful feedback below.

2
  • this is not working. I even changed "<%= HttpContext...." to "<%# HttpContext..." to avoid the errors, changed web.config to set compilation debug="false" and ran in release mode, but the control is still visible. Jun 1, 2011 at 17:22
  • You have to call DataBind() if you use <%# %> notation.
    – mathieu
    Jun 5, 2011 at 19:47
1

I bet you can make it work with a

#if DEBUG
#endif 

bit of code in your ASPX page, not your code-behind (that's a separate compile).

Something like:

<script runat="server" language="C#">
  protected Page_Load() {
#if DEBUG
     myid.Visible = true;
#else
     myid.Visible = false;
#endif
  }
</script>

Alternatively, you could us ConfigurationManager or XElement and actually parse the web.config from code and find the attribute.

For example:

var xml = XElement.Load("path-to-web.config");
bool isDebug = (bool)xml.Descendants("compilation").Attribute("debug");
2
  • I am not looking for compile time directives Jun 1, 2011 at 17:23
  • Sure, the XElement bit has nothing to do with compiler directives. Jun 1, 2011 at 18:30
0

In your code, you could use an IF DEBUG pre-processor directive to set the visibility attribute:

http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

Good article from Phil Haack on this:

http://haacked.com/archive/2007/09/16/conditional-compilation-constants-and-asp.net.aspx#51205

2
  • 1
    Yes, that's right. But only in the aspx view, because debug build in the DLL is different than in the web.config. May 31, 2011 at 20:17
  • Good point, I've added a link above that goes into detail on this :) May 31, 2011 at 20:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.