Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The DEBUG constant in Visual Studio 2012 won't activate

Trying to use the ifdef DEBUG constant in Visual Studio doesn't work for me in a .NET Framework 4.5, ASP.NET MVC 4.

I made sure:

  • The "Define DEBUG constant" is checked for the Debug configuration in project properties
  • The Web.config does include the <compilation debug="true"...

The code I use to verify shows isDebug = false, both in the IDE and in runtime, even though i run under the Debug-configuration:

bool isDebug;
#if DEBUG
    isDebug = true;
#else
    isDebug = false;
#endif

This is a screen cap of Visual Studio, showing i have the Debug-configuration active, but in the code, the color highlighting says that DEBUG is not defined.

if DEBUG isDebug=true else isDebug=false

I must be missing something!

Edit: Screen cap of what the properties window shows for the Build-view: Properties-window

like image 315
cederlof Avatar asked Oct 30 '13 12:10

cederlof


2 Answers

No. You cannot check the DEBUG constant with #if DEBUG in a file with extension .cshtml

like image 68
helb Avatar answered Nov 15 '22 18:11

helb


This is by design.

These directives are compile time only. CSHTML (and other ASP.NET pages) are compiled at runtime (on the web server) so the directive you have selected in VS will not affect them.

See the post CSHTML does not recognize project level compilation symbols on Microsoft Connect

like image 34
Jehof Avatar answered Nov 15 '22 18:11

Jehof