简体   繁体   English

我能否确定Visual Studio 2012何时处于“调试与生产”阶段?

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

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. 而是,我想知道开发人员是否已从Visual Studio 2012“标准”工具栏中的配置管理器下拉列表中选择了“调试”还是“生产”。

Why? 为什么? I have a Pre-build step which conditionally compiles some JavaScript and CSS based upon the "ConfigurationName". 我有一个预构建步骤,该步骤根据“ ConfigurationName”有条件地编译一些JavaScript和CSS。

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. ...但是这不起作用(由于多种原因),而我的C#/ ASP.NET知识在这方面简直是缺乏。

Help? 救命?

bool isInDebug = false;

#if DEBUG
    isInDebug = true;
#endif

Use the #if directive reference to accomplish what you're looking for. 使用#if指令参考来完成您要寻找的东西。

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

You can use the if directive references to distinguish production vs debug. 您可以使用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
    }
}

While all of the answers you gave (basically the same thing) are true, I can't post that logic inside my View. 尽管您给出的所有答案(基本上是同一件事)都是正确的,但我无法在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. 我看到了这个答案 ,其中有一条评论说可以尝试将指令添加到控制器,然后设置一些ViewData,可以在我的View中用作条件检查。

    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! 这就像一个魅力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM