C# and ASP.NET MVC: Using #if directive in a view

I recently discovered that you can simply test:

HttpContext.Current.IsDebuggingEnabled

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


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())
...

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>
<% } %>