簡體   English   中英

我能否確定Visual Studio 2012何時處於“調試與生產”階段?

[英]Can I tell when Visual Studio 2012 is in Debug vs Production?

我有一個視圖,要在其中有條件地顯示兩個文件之一中的代碼:

<% 
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知識在這方面簡直是缺乏。

救命?

bool isInDebug = false;

#if DEBUG
    isInDebug = true;
#endif

使用#if指令參考來完成您要尋找的東西。

#define DEBUG
// ...
#if DEBUG
    Console.WriteLine("Debug version");
#endif

您可以使用if指令引用來區分生產和調試。

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

盡管您給出的所有答案(基本上是同一件事)都是正確的,但我無法在View中發布該邏輯。 我看到了這個答案 ,其中有一條評論說可以嘗試將指令添加到控制器,然后設置一些ViewData,可以在我的View中用作條件檢查。

    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");
} 
%>

這就像一個魅力!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM