是否有针对Asp.net标记的#IF DEBUG?

时间:2008-12-18 19:45:12

标签: asp.net debugging

基于“定义DEBUG常量”设置,我可以在asp.net页面的标记中执行类似的操作吗?

#IF (DEBUG) THEN
  <asp:TextBox ID="TextBox1" runat="server">You're in debug mode</asp:TextBox>
#END IF

5 个答案:

答案 0 :(得分:58)

<form runat="server">
 <% #if DEBUG %>
 <asp:TextBox ID="TextBox1" runat="server">You're in debug mode</asp:TextBox>
 <% #else %>
 <asp:TextBox ID="TextBox2" runat="server">Mmm... No, I think you're not in debug mode</asp:TextBox>
 <% #endif %>
</form>

请注意,您无法为这些文本框分配相同的ID。

另请注意,在web.config中设置DEBUG时为真:

<compilation debug="true">

答案 1 :(得分:6)

我能得到的结果是:

<asp:Literal id="isDebug" runat="server" />
<script runat="server">
    void Page_Load()
    {
#if DEBUG
        isDebug.Text = "You're in debug mode";
#endif
    }
</script> 

如果你想在Page_Load()事件中有任何其他内容,这会给你带来麻烦;上面的文字代码仅在页面/控件后面没有代码时才有效。

如果我需要这样做,我会将上面的代码包含在用户控件中,并将该控件包含在感兴趣的页面中。

我的测试用户控件如下所示:

<%@ Control Language="C#" AutoEventWireup="true"  %>
<asp:Literal id="isDebug" runat="server" />
<script runat="server">    
    void Page_Load()    
    {
#if DEBUG        
        isDebug.Text = "You're in debug mode";
#endif    
    }
</script> 

答案 2 :(得分:6)

如果您尝试单步执行javascript或者在不调试时更喜欢缩小javascript,我更喜欢这种方法:

<% if (Debugger.IsAttached) { %>

  <script src="jquery.js"></script>

<% } else { %>

  <script src="jquery.min.js"></script>

<% } %>

我在调试时可以轻松单步执行代码,否则我希望缩小脚本。请务必包含以下导入:

<%@ Import Namespace="System.Diagnostics" %>

此外,最好使用Web Essentials visual studio扩展来捆绑/缩小您的javascript文件,以便只为服务器提供一个脚本请求。

答案 3 :(得分:0)

如何使用Literal然后在代码隐藏中使用#if DEBUG将文本框的html注入文字? ASP.NET中还有直接的代码块,但我不知道它们是否处理#if语句;那些似乎是为C#编译器保留的。

答案 4 :(得分:-1)

滚动你自己很容易。您可能会错过编译常量的一些较冷的非编译功能,但您肯定能够根据某种全局参数添加标记。

相关问题