2014-01-28 24 views
0

我有,我希望有條件地從兩個文件之一顯示代碼視圖:我可以告訴Visual Studio 2012何時處於調試階段與生產階段?

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

上面的代碼工作......但我其實是,如果調試器附加不太感興趣。相反,我想知道開發人員是否從Visual Studio 2012「標準」工具欄的配置管理器下拉菜單中選擇了「調試」或「生產」。

爲什麼?我有一個預構建步驟,它根據「ConfigurationName」有條件地編譯一些JavaScript和CSS。

我試圖用這樣的:

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

...但不工作(對於各種原因),我的C#/ ASP.NET知識根本缺乏在這個領域。

幫助?

回答

0

雖然你給出的所有答案(基本上是同一件事)都是真的,但我無法在View中發佈該邏輯。我看到this answer其中一條評論說試圖將指令添加到控制器,然後設置一些可用於我的視圖的ViewData作爲條件檢查。

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

     #if DEBUG 
      status = "Debug"; 
     #endif 

     ViewData["ConfigurationStatus"] = status; 

     return View(); 
    } 

在我看來......

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

這就像一個魅力!

1

使用#if directive參考來完成您正在尋找的內容。

#define DEBUG 
// ... 
#if DEBUG 
    Console.WriteLine("Debug version"); 
#endif 
2
bool isInDebug = false; 

#if DEBUG 
    isInDebug = true; 
#endif 
1

您可以使用,如果指令引用來區分生產VS調試。

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