33

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?) %>" />
mathieu
  • 30,573
  • 4
  • 60
  • 89
  • 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; – Outside the Box Developer Jun 01 '11 at 17:35

5 Answers5

58

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.

Jordan Gray
  • 15,876
  • 3
  • 50
  • 66
Adrian Salazar
  • 5,419
  • 30
  • 49
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!

William
  • 7,808
  • 5
  • 37
  • 42
Nicholas Carey
  • 65,549
  • 13
  • 92
  • 133
  • 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 04 '13 at 03:47
  • 11
    Why not HttpContext.Current.IsDebuggingEnabled ? – Adrian Salazar Mar 25 '13 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. –  May 27 '13 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. – Adrian Salazar May 28 '13 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.

Peter Ivan
  • 1,381
  • 2
  • 13
  • 26
mathieu
  • 30,573
  • 4
  • 60
  • 89
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

IrishChieftain
  • 15,099
  • 7
  • 48
  • 91
0

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");
Michael Kennedy
  • 2,964
  • 2
  • 25
  • 33