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?) %>" />
mathieu
  • 30,974
  • 4
  • 64
  • 90
  • 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

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.

Jordan Gray
  • 16,306
  • 3
  • 53
  • 69
Adrian Salazar
  • 5,279
  • 34
  • 51
  • available in all .NET versions it looks like.. http://msdn.microsoft.com/en-us/library/system.web.httpcontext.isdebuggingenabled%28v=vs.100%29.aspx – diegohb Oct 21 '13 at 19:59
  • but not available in every context e.g. WCF – DATEx2 Apr 03 '19 at 16:44
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
  • 8,007
  • 5
  • 39
  • 43
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • 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,467
  • 2
  • 14
  • 27
mathieu
  • 30,974
  • 4
  • 64
  • 90
  • 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. – Outside the Box Developer Jun 01 '11 at 17:22
  • You have to call DataBind() if you use <%# %> notation. – mathieu Jun 05 '11 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");
Michael Kennedy
  • 3,202
  • 2
  • 25
  • 34
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,108
  • 7
  • 50
  • 91