Conditional Compilation In ASP.NET 2.0

Friday, December 2, 2005

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.


Comments
Bill Fields Thursday, December 29, 2005
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.
scott Friday, December 30, 2005
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.
ArvinBoggs Sunday, March 12, 2006
So, how to adjust the "trust" to "allow me to fiddle with compiler options"?
scott Monday, March 13, 2006
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.
gravatar Saad Wali Jaan Thursday, October 8, 2009
Hi,
I have to do conditional compiling at few places in my application. At present i have declared variables and change them manually at all the places. I need to move that variable in web.config and do conditional compiling based on the value set in the web.config so that i dont need to change it all the places. In case Yes, please tell me how? I am using vb.net.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!