How to determine if compilation debug="true" in web.config

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?) %>" />

Upvotes: 36

Views: 24597

Answers (5)

Adrian Salazar
Adrian Salazar

Reputation: 5319

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.

Upvotes: 61

Nicholas Carey
Nicholas Carey

Reputation: 74227

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!

Upvotes: 54

mathieu
mathieu

Reputation: 31192

<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.

Upvotes: 15

IrishChieftain
IrishChieftain

Reputation: 15253

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

Upvotes: 0

Michael Kennedy
Michael Kennedy

Reputation: 3380

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");

Upvotes: 1

Related Questions