Wednesday, July 16, 2008

Conditional Compilation in ASP.Net WebSite projects

In ASP.Net 2.0 if you create a Web-Site project, then you don't have a way to specify "Conditonal Compilation Constants" as Web Site projects don't have a properties page. So what do you do if you need to use Conditional Compilation Constants such as DEBUG, RELEASE, etc to perform conditional compilation?

Here is the only way I found out how to do this:

You need to edit the Web.Config file for the project and add the following section under Configuration:

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

 

If you already have a system.codedom section (which is the case with Visual Studio 2008), then just copy the compiler section from above into your Web.Config file.

The following is how the system.codedom section looks in VS 2008.

<system.codedom>
      <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <providerOption name="CompilerVersion" value="v3.5"/>
          <providerOption name="WarnAsError" value="false"/>
        </compiler>
        <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4"
                  type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <providerOption name="CompilerVersion" value="v3.5"/>
          <providerOption name="OptionInfer" value="true"/>
          <providerOption name="WarnAsError" value="false"/>
        </compiler>
          <compiler
               language="c#;cs;csharp" extension=".cs"
               compilerOptions="/d:RELEASE"
               type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </compilers>
    </system.codedom>

 

Finally to add your own Conditional Compilation Constants, add it to the compilerOptions line, seperating each constant with a semi-colan (;).

No comments: