在aspx页面中使用#if DEBUG条件编译语句

时间:2010-03-09 15:46:09

标签: webforms conditional-compilation

我试图在aspx页面中做这样的事情:

<head runat="server">
    <% #if DEBUG %>
        <script src="jquery-1.3.2.js" type="text/javascript"></script>
    <% #else  %>
        <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
    <% #endif %>
</head>

我收到错误“预处理程序指令必须显示为一行中的第一个非空白字符”。我怎么能这样做?

2 个答案:

答案 0 :(得分:6)

<head runat="server">
  <% 
    #if DEBUG
  %>
    <script src="jquery-1.3.2.js" type="text/javascript"></script>
  <%
    #else
  %>
    <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
  <%
    #endif
  %>
</head>

适合我 - 请注意,这是基于web.config的debug元素中<compilation>属性的值。

编辑以回复评论

啊,所以你也通过代码隐藏添加控件到头部?然后你可能需要从代码隐藏中动态添加它。

如果您乐意始终提供缩小版本,但想在Visual Studio中使用IntelliSense,则应确保已安装此修补程序以启用此功能:

  

VS2008 SP1 Hotfix to Support "-vsdoc.js" IntelliSense Doc Files

这将使您能够命名您的非缩小版本jquery-1.3.2.min-vsdoc.js并让VS在您构建页面时读取该缩小版本。

答案 1 :(得分:1)

这对我有用:

&#13;
&#13;
<head runat="server">
    <asp:PlaceHolder runat="server">
    <% 
#if !DEBUG 
    %>
    <meta http-equiv="X-UA-Compatible" content="IE=9" />
    <% 
#else 
    %>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <% 
#endif 
    %>
    </asp:PlaceHolder>
</head>
&#13;
&#13;
&#13;