views:

178

answers:

2

Hi all,

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.

+2  A: 

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>
<% } %>
Developer Art
I'm gathering from this answer that it's not possible to check compilation symbols from a view, and that is must be done from a .cs file. Is this true?
Mega Matt
@DeveloperArt Nice solution, but what about placing this logic in a base controller that all Controllers inherit. Then you could have access of this property through all Controllers...
Alexander
well, not sure that would completely work xander. To have access to it from the view, we would need a Model with that property, not a Controller with that property. So I'd really have to pass that same Model from multiple controllers and render that view, which would be sort of a pain.
Mega Matt
If you place that logic in a base controller and access it from a derived controller then you could assign it to `ViewData` or some custom `ViewModel`
Alexander
So actually, what I'm going to do is set a property in one of our Global files, which should solve the problem. Same general principle as Develop Art's solution, but slightly tweaked. Thanks for the responses.
Mega Matt
A: 

Rather than check the RELEASE symbol, check the DEBUG symbol as follows:

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

Then you can control which banner is rendered via your web.config, not the compilation mode of your assemblies. Your view is compiled by ASP.NET and so respects the debug setting in either the Control directive or the web.config file.

From an ASCX file:

<%@ Control Language="C#" Debug="true" Inherits="System.Web.Mvc.ViewUserControl" %>

From web.config:

<compilation defaultLanguage="c#" debug="true">

Note that this only tells you how to do what you're asking. Whether or not you should do what you're asking is another question. ;-)

John Bledsoe