在.aspx文件中进行条件编译

Rad*_*094 5 asp.net

我需要.ASPX文件基于条件编译符号表现不同.

让我们说一个简单的例子:

<%@ Control Language="C#" AutoEventWireup="true" (...) %>
<% #ifdef DEBUG %>
     <asp:SomeDebugControlHere runat="server"/>
     .. well .. a LOT of code here
<% #else %>
    <asp:SomeReleaseControlHere runat="server"/>
    .. and a LOT of other code here
<% #endif %>
Run Code Online (Sandbox Code Playgroud)

后来编辑:更多澄清.问题是SomeDebugControlHere类甚至没有在发布版本中定义(它在现实生活中更复杂,但是请注意这个例子).所以在page.aspx.designer.cs中我需要在调试版本中得到它:

/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.SomeDebugControlHere myControl
Run Code Online (Sandbox Code Playgroud)

这在发布版本中:(并且从不都是)

/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.SomeReleaseControlHere myControl
Run Code Online (Sandbox Code Playgroud)

显然我需要ASPX文件中的标记不同,但我还需要修改designer.cs文件以合并新的对象/类.

我只是希望有人知道一种聪明的方法,通过一些控制继承来做这件事,任何允许我根据编译构建设置指定不同控件类的东西.

Nic*_*ver 6

你只需要一个调整,应该是:

<% #if DEBUG %>
Run Code Online (Sandbox Code Playgroud)

如果你希望它表现得像一组C#代码那样.


Rad*_*094 1

使用编译符号进行编译时似乎存在差异,具体取决于声明它们的位置:作为项目属性、使用 @Page 指令或在 web.config 中。

只有web.config中定义的符号似乎可以在 .aspx 文件中使用。

<compiler
      language="c#;cs;csharp" extension=".cs"
      compilerOptions="/d:MY_CONSTANT"
      type="Microsoft.CSharp.CSharpCodeProvider, 
        System, Version=2.0.0.0, Culture=neutral, 
        PublicKeyToken=b77a5c561934e089" />
    </compilers>
Run Code Online (Sandbox Code Playgroud)

在这里这里找到了一些关于此的信息