如何确定web.config中的编译debug =“true”

我在这里画一个空白的东西应该很简单……

我想做的事情如下:

<my:control runat="server" id="myid" Visible="" /> 

这应该会得到部分组中的元素:

 using System.Web.Configuration ; . . . CompilationSection compilationSection = (CompilationSection)System.Configuration.ConfigurationManager.GetSection(@"system.web/compilation") ; . . . // check the DEBUG attribute on the  element bool isDebugEnabled = compilationSection.Debug ; 

简单!

HttpContext.IsDebuggingEnabled属性 :

 using System.Web; if (HttpContext.Current.IsDebuggingEnabled) { /* ... */ } 

从文档:

如果请求处于调试模式,则获取一个值,该值指示当前HTTP请求是否处于调试模式[…]为true ; 否则,是的。

  

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

或http://www.west-wind.com/weblog/posts/2007/Jan/19/Detecting-ASPNET-Debug-mode ,下面有丰富的反馈意见。

我打赌你可以用它来工作

 #if DEBUG #endif 

ASPX页面中的一些代码,而不是代码隐藏(这是一个单独的编译)。

就像是:

  

或者,您可以使用ConfigurationManagerXElement实际解析代码中的web.config并查找属性。

例如:

 var xml = XElement.Load("path-to-web.config"); bool isDebug = (bool)xml.Descendants("compilation").Attribute("debug"); 

在您的代码中,您可以使用IF DEBUG预处理器指令来设置可见性属性:

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

来自Phil Haack的好文章:

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