繁体   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