Conditional Compilation In ASP.NET 2.0

Conditional compilation allows you to select sections of code to include or exclude from compilation depending on the presence of a constant. This feature is available in both C# and VB.NET. The following page needs DEBUG and MYCONSTANT defined to execute both Response.Write methods at runtime.

protected void Page_Load(object sender, EventArgs e)
{
    #if DEBUG
    Response.Write("DEBUG is defined <br/>");
    #endif

    #if
MYCONSTANT
    Response.Write(
"MYCONSTANT is defined <br />");
    #endif
}

In 1.1 it was easy to define constants for a web project, but there is no project property page with this option in 2.0. The ASP.NET platform controls compilation, so we need to feed ASP.NET the information.

First, ASP.NET will define a DEBUG constant when debug=”true” in the <compilation> section of web.config. Unfortunately, the IDE isn't smart enough to detect when DEBUG is defined. If the IDE thinks code is excluded from compilation, it greys out the text.

For your own constants, it gets a little trickier. For example, to define MYCONSTANT there are two options. One option is to define MYCONSTANT for a single page at a time using CompilerOptions in the page directive like so (/d works for csc and vbc compilers):

<%@ Page Language="C#" ... CompilerOptions="/d:MYCONSTANT" %>

Of course, you probably want all code in the web project to see the constant, so you can change the compiler options in web.config.

<system.codedom>
   <
compilers>
      <
compiler             
         
language="c#;cs;csharp" extension=".cs" 
         
compilerOptions="/d:MYCONSTANT"
         type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />           
   </
compilers>
</
system.codedom>

Having said all this, the default settings for medium trust do not allow you to fiddle with compiler options (I imagine this is to prevent someone from throwing the /unsafe switch, for example). Keep this in mind if you use conditional compilation and rely on dynamic compilation in a hosted environment.

posted on Thursday, December 01, 2005 7:06 PM by scott

Comments

Thursday, December 01, 2005 7:30 PM by Keyvan Nayyeri

# Compilation in ASP.NET 2.0

Compilation in ASP.NET 1.x was based on DLL files. So&amp;nbsp;it was necessary for all developers to compile...
Saturday, December 03, 2005 5:17 AM by Thomas goes .NET

# Conditional Compilation in ASP.NET

Thursday, December 08, 2005 7:01 PM by Christopher Steen

# Link Listing - December 8, 2005

[tip] localhost vs. (local) in SQL Server connection
strings [Via: Jon
Galloway ]
ASP.NET 2.0 Upgrade...
Thursday, December 29, 2005 1:45 PM by Bill Fields

# re: Conditional Compilation In ASP.NET 2.0

I'd like to figure out how to have conditional web.config sections. For example, one connection string when I'm on my development system and another when the app is running in it's hosted environment.
Thursday, December 29, 2005 7:29 PM by scott

# re: Conditional Compilation In ASP.NET 2.0

Bill:

What you want to do is doable. You want to use a configSource the the connetionStrings. See: http://odetocode.com/Articles/418.aspx

Hope that helps.
Sunday, March 12, 2006 2:36 PM by ArvinBoggs

# re: Conditional Compilation In ASP.NET 2.0

So, how to adjust the "trust" to "allow me to fiddle with compiler options"?
Sunday, March 12, 2006 5:08 PM by scott

# re: Conditional Compilation In ASP.NET 2.0

Arvin:
Set the trust element to full trust. Full trust is the default, so if you aren't running under full trust someone changed machine.config or web.config.
Friday, May 12, 2006 11:45 PM by mike's web log

# Response.DontWrite

Some thoughts on peeking under the hood while the engine is running.&lt;br /&gt;&lt;br /&gt;How do you know what's going on inside your ASP.NET code? Here are the ways I know about:&lt;br /&gt;&lt;br /&gt;Response.Write - This time-honored method was the standby of folks codi ...
Friday, July 14, 2006 10:58 PM by Keyvan Nayyeri

# Compilation in ASP.NET 2.0

Compilation in ASP.NET 1.x was based on DLL files. Soit was necessary for all developers to compile their
Sunday, July 16, 2006 1:23 AM by Keyvan Nayyeri

# Compilation in ASP.NET 2.0

Compilation in ASP.NET 1.x was based on DLL files. Soit was necessary for all developers to compile their
Sunday, September 16, 2007 10:28 PM by you've been HAACKED

# Conditional Compilation Constants and ASP.NET

Conditional Compilation Constants and ASP.NET
Sunday, September 16, 2007 10:42 PM by Community Blogs

# Conditional Compilation Constants and ASP.NET

Conditional compilation constants are pretty useful for targeting your application for a particular platform