Conditionally compile into .aspx files

I need a .ASPX file to behave differently based on a conditional compilation symbol.

Say as a simple example:

<%@ 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 %> 

Later Edit : A few more clarifications. The problem is that the SomeDebugControlHere class is not even defined in the release build (it is more complex in real life, but it is carried in this example). So in the page.aspx.designer.cs file, I need to get this in the debug assembly:

 /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.SomeDebugControlHere myControl 

and this is in the release build: (and never both)

 /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.SomeReleaseControlHere myControl 

Obviously, I need markup in the .aspx file to be different, but I need the designer.cs file to be modified to include new objects / classes as well.

I just hope someone knows of a smart way to do this, either with some kind of control inheritance, or something that will allow me to specify different control classes depending on the compilation build settings.

+4
source share
3 answers

It seems that there is a difference when compiling with compilation characters depending on where you declare them: as a project property, using the @Page directive or in web.config.

In .aspx files, only characters defined in web.config work .

 <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> 

Information found about it here and here.

+1
source

If you need customization, then it should be:

 <% #if DEBUG %> 

If you want it to behave like a set of C # codes, there will be.

+5
source

I think that to some extent the answer lies in the auto-generated comments in the constructor file:

 /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.SomeDebugControlHere myControl 

You should probably transfer the declaration of these controls to your code file and wrap them in accordance with the #ifdef instructions.

+1
source

Source: https://habr.com/ru/post/1303551/


All Articles