【问题标题】:How to determine if compilation debug="true" in web.config如何确定 web.config 中的编译 debug="true"
【发布时间】:2011-05-31 20:12:52
【问题描述】:

我在这里为应该简单的东西画了一个空白......

我正在尝试做类似的事情:

    <my:control runat="server" id="myid" Visible="<%= (is compilation debug mode?) %>" />

【问题讨论】:

  • 不完全是我想要的(不想使用后面的代码或指令),但我能够在后面的代码中使用,来自 Nicholas Carey 的代码:DebugViewControl.Visible = ((System .Web.Configuration.CompilationSection)ConfigurationManager.GetSection(@"system.web/compilation")).Debug;

标签: c# asp.net debugging


【解决方案1】:

HttpContext.IsDebuggingEnabled property:

using System.Web;

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

来自文档:

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

【讨论】:

【解决方案2】:

这应该会为您提供&lt;system.web&gt; 部分组中的&lt;compilation&gt; 元素:

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 ;

简单!

【讨论】:

  • 如此简单,只需将它发送给一个试图在调试构建中关闭他的缩小代码的队友。谢谢:)
  • 为什么不使用 HttpContext.Current.IsDebuggingEnabled ?
  • @AdrianSalazar 在某些情况下 HttpContext 可能为空,例如运行与上下文分开运行的异步后台线程的进程。
  • @ErgonomicDeveloper 很好,请参考问题本身。我们正在渲染一个控件,而不是运行异步代码。这个线程会有一个HttpContext,所以我不用担心。
【解决方案3】:
<my:control runat="server" id="myid" Visible="<%= HttpContext.Current.IsDebuggingEnabled %>" />

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 下方有丰富的反馈。

【讨论】:

  • 这不起作用。我什至将 "
  • 如果使用 表示法,则必须调用 DataBind()。
【解决方案4】:

我敢打赌,您可以使用

#if DEBUG
#endif 

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

类似:

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

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

例如:

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

【讨论】:

  • 我不是在寻找编译时指令
  • 当然,XElement 位与编译器指令无关。
【解决方案5】:

在您的代码中,您可以使用 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

【讨论】:

  • 是的,没错。但仅在 aspx 视图中,因为 DLL 中的调试构建与 web.config 中的不同。
  • 好点,我在上面添加了一个链接,详细介绍了这个:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多