46
votes

I've got a conditional compilation symbol I'm using called "RELEASE", that I indicated in my project's properties in Visual Studio. I want some particular CSS to be applied to elements when the RELEASE symbol is defined, and I was trying to do that from the view, but it doesn't seem to be working.

My view code looks like this (shortened a bit for demo purposes):

<% #if (RELEASE) %>
    <div class="releaseBanner">Banner text here</div>
<% #else %>
    <div class="debugBanner">Banner text here</div>
<% #endif %>

With this code, and with the RELEASE symbol set, the 'else' code is running and I'm getting a div with the debugBanner class. So it doesn't seem to think that RELEASE is defined. It's worth noting that my actual C# code in .cs files is recognizing RELEASE and runs the correct code. It's only the view that is giving me the problem.

Does anyone have any insight into this? Any help would be appreciated. Thanks.

Clarification: I should have mentioned that this view is already a partial view, and I'll simply render it in pages where I need it. That's because these banners will be on certain pages and not others. So even when rendering it as a partial view via:

Html.RenderPartial("BannerView");

it's not working.

8
Why don't you create a html helper method and put your code in there? You can then do the if and do a render partial to keep the HTML - Rippo
Not too familiar with html helper methods. I did clarify my question to indicate that this is a partial view I'm working with. Can you elaborate a bit on your suggestion? - MegaMatt
preprocessor directives only work @ compile-time... @DeveloperArt has an elegant solution... - xandercoded
Adding the keyword "constant" to this page so it gets returned for searches for "compiler constants". (hopefully) - G-Wiz
See: stackoverflow.com/a/8325519/204699 for another alternative to using conditional compilation in views. - João Angelo

8 Answers

18
votes

In your model:

bool isRelease = false;

<% #if (RELEASE) %>
    isRelease = true;
<% #endif %>

In your view:

<% if (Model.isRelease) { %>
    <div class="releaseBanner">Banner text here</div>
<% } else { %>
    <div class="debugBanner">Banner text here</div>
<% } %>
107
votes

I recently discovered that you can simply test:

HttpContext.Current.IsDebuggingEnabled

in Views, which saves you checking symbols in other parts of your app.

61
votes

A better, more generic solution is to use an extension method, so all views have access to it:

public static bool IsReleaseBuild(this HtmlHelper helper)
{
#if DEBUG
    return false;
#else
    return true;
#endif
}

You can then use it like follows in any view (razor syntax):

@if(Html.IsReleaseBuild())
...
9
votes
@if (HttpContext.Current.IsDebuggingEnabled)
{
    // Debug mode enabled. Your code here. Texts enclosed with <text> tag
}
3
votes

You can use ViewBag instead of viewmodel (but viewmodel-like approach is better) :

Controller :

controller code

View :

@{
   bool hideYoutubeVideos = ViewBag.hideYoutubeVideos ?? false;     
}

Usage :

@if (!hideYoutubeVideos)
{
     <span>hello youtube</span>
}

Also, be sure, that NIKITA_DEBUG variable exist in build tab of your project :

build tab

1
votes

For me, the code below has worked very well. When the application is Debugging my buttons appear, when is Release, don't.

@if (this.Context.IsDebuggingEnabled)
{
    <button type="button" class="btn btn-warning">Fill file</button>
    <button type="button" class="btn btn-info">Export file</button>
} 
0
votes

You can use Debugger.IsAttached like this:

@using System.Diagnostics

@{
    string gridID = $"the-grid-7";

    if (Debugger.IsAttached)
        gridID = gridID + new Random().Next(1, 1000).ToString();

    var loadUrl = ViewBag.LoadUrl;
}
-5
votes

Below is the Razor syntax for conditional compiler directives. It loads the developer version of jquery when DEBUG variable is set in VS profile or web.config. Otherwise the min version is loaded.

    @{
#if (DEBUG)
    }
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.js"></script>
    @{    
#else
    }
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>
    @{
#endif
    }