0

I have a View in which I want to conditionally display code from one of two files:

<% 
if (System.Diagnostics.Debugger.IsAttached) {
    Response.WriteFile("~/path/to/index-A.html");
} else {
    Response.WriteFile("~/path/to/index-B.html");
} 
%>

The above code works... but I'm actually less interested if the debugger is attached. Rather, I want to know whether the developer has selected "Debug" or "Production" from the Configuration Manager drop-down in the Visual Studio 2012 "Standard" toolbar.

Why? I have a Pre-build step which conditionally compiles some JavaScript and CSS based upon the "ConfigurationName".

I was trying to use something like this:

if (System.Configuration.ConfigurationManager == "Debug") { //...

...but that doesn't work (for a variety of reasons) and my C#/ASP.NET knowledge simply lacks in this area.

Help?

arthurakay
  • 5,631
  • 8
  • 37
  • 62

4 Answers4

2
bool isInDebug = false;

#if DEBUG
    isInDebug = true;
#endif
pid
  • 11,472
  • 6
  • 34
  • 63
1

Use the #if directive reference to accomplish what you're looking for.

#define DEBUG
// ...
#if DEBUG
    Console.WriteLine("Debug version");
#endif
Cam Bruce
  • 5,632
  • 19
  • 34
1

You can use the if directive references to distinguish production vs debug.

// preprocessor_if.cs
#define DEBUG 
#define MYTEST
using System;
public class MyClass 
{
    static void Main() 
    {
#if (DEBUG && !MYTEST)
        Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && MYTEST)
        Console.WriteLine("MYTEST is defined");
#elif (DEBUG && MYTEST)
        Console.WriteLine("DEBUG and MYTEST are defined");
#else
        Console.WriteLine("DEBUG and MYTEST are not defined");
#endif
    }
}
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
0

While all of the answers you gave (basically the same thing) are true, I can't post that logic inside my View. I saw this answer where a comment said to try adding the directives to the controller, and then setting some ViewData which could be used in my View as a conditional check.

    public ActionResult Index()
    {
        string status = "Release";

        #if DEBUG
            status = "Debug";
        #endif

        ViewData["ConfigurationStatus"] = status;

        return View();
    }

and in my view...

<% 
if (ViewData["ConfigurationStatus"] == "Debug") {
    Response.WriteFile("~/path/to/index-A.html");
} else {
    Response.WriteFile("~/path/to/index-B.html");
} 
%>

This works like a charm!

Community
  • 1
  • 1
arthurakay
  • 5,631
  • 8
  • 37
  • 62