The DEBUG constant in Visual Studio 2012 won't activate
Asked Answered
R

2

9

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

Risarise answered 30/10, 2013 at 12:17 Comment(15)
please verify that where you have checked "Define DEBUG constant", the configuration dropdown is set to "Debug"Corydalis
@RobertLevy Thanks. I verified it again, and it is on the Debug-configuration (inside the properties window).Risarise
What is the Active config property value for your solution?Mordancy
@AlexFilipovici, good question; but it appears from the screenshot Debug.Gile
@neoistheone, thanks, the image URL is blocked in my current location.Mordancy
@AlexFilipovici It is debug. (For those that can see images ;) I added one screen cap of the properties window as well.)Risarise
@cederlof, are you sure you're working in the same project you're looking at the properties in?Gile
Have you tried to use another conditional compilation symbol? For example MYDEBUG?Mordancy
@neoistheone Yes, I'm sure. (Even though I have another MVC-proj in the same solution.)Risarise
have you saved the project file? hitting Ctrl+Shift+S or by reopening the solution.Valenza
@AlexFilipovici Yes, I did. It doesn't work (in the same way as DEBUG) if I define one of my own.Risarise
Can you use #if DEBUG in a file with extension .cshtml ??Fleece
@Valenza Yes, I have restarted Visual Studio a few times.Risarise
@Fleece Bingo! Post it as an answer so I can accept it. Thanks to all!Risarise
What about MvcBuildViews? Does it matter?Cestoid
F
8

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

Fleece answered 30/10, 2013 at 12:49 Comment(1)
Anyone care to find out why this is the case? :) Maybe the ASP.NET compiler doesn't support conditional constants?Cestoid
V
3

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

Valenza answered 30/10, 2013 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.