3

I want to be able to point to one of 2 assemblies based on what mode (DEBUG or RELEASE) I have selected in my VS2005 IDE. Something like this (which does not work):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VideoDialog.ascx.cs" Inherits="Company.Web.Base.Controls.VideoDialog" %>

<% #if DEBUG %>
<%@ Register TagPrefix="Company" Assembly="Company" Namespace="Company.UI.Controls.VideoControl" %>
<% #else %>
<%@ Register TagPrefix="Company" Assembly="Company.UI.Controls.VideoControl" Namespace="Company.UI.Controls.VideoControl" %>
<% #endif %>

<Company:CompanyVideo ID="Video1" runat="server"></Company:CompanyVideo>

So, my question is: How do I correctly use a #if DEBUG in an ASPX or ASCX page?

Keith Barrows
  • 24,802
  • 26
  • 88
  • 134
  • For the record, what you have works outside of the control references. I've used almost the exact same technique to dynamically control what is in my javascript and it works perfectly. So your problem has to do with the control references. – Erik Allen Jun 20 '13 at 19:01

3 Answers3

3

I don't know how to get what you want, but I face the same problem. I do my control references in web.config and then do post build steps to copy the appropriate web.config for release/debug. It works because you need a different web.config for release/debug anyhow (if only for the debug="true" attribute) and because you can have a different post build step for debug and release.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • Do you have any examples of how you do this? Another one of my tasks will be to introduce automatic builds to this company and I've done very little outside of a very basic build. – Keith Barrows Jun 25 '09 at 22:10
  • 1
    The magic is if "$(ConfigurationName)" == "Debug" ... batch file... More info: found SO Question with some advice http://stackoverflow.com/questions/150053/how-to-run-visual-studio-post-build-events-for-debug-build-only and the MSDN Docs: http://msdn.microsoft.com/en-us/library/ke5z92ks.aspx and a blog entry http://www.adduxis.com/blogs/blogs/sven/archive/2005/11/01/15.aspx – MatthewMartin Jun 26 '09 at 00:15
3
<%
//<compilation debug="false"> in web.config
//*.aspx

#if DEBUG
    Response.Write("<script type=\"text/javascript\">");
    Response.Write("$.validator.setDefaults({ debug: true })");
    Response.Write("</script>");
#endif

%>
sth
  • 222,467
  • 53
  • 283
  • 367
ohaiyo
  • 31
  • 1
2

Another approach is to use HtmlHelper extension method. Basically you code a C# file with something like this:

namespace ExtensionHandlers
{
    public static class MetaTags
    {
        public static string GetMetaTags(this HtmlHelper html)
        {
            #if DEBUG

            return string1;

            #else

            return string2;

            #endif
        }
    }
}

Then in your ascx file import the file:

<%@ Import Namespace="ExtensionHandlers" %>

And finally where you want the code just do this:

<%= Html.GetMetaTags() %>

Disclaimer: I have not compiled this, there are probably coding errors. Good luck.

Anemoia
  • 7,928
  • 7
  • 46
  • 71
Jason
  • 21
  • 1