“#if DEBUG” in an ASPX/ASCX page
-
20-08-2019 - |
Question
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?
Solution
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.
OTHER TIPS
<%
//<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
%>
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.