从调试版本中检测发布版本的最佳方法? .net

问题描述:

所以我有大约10个简短的css文件,我用mvc应用程序。 有像 error.css login.css etc ... 只是一些非常简短的css文件,使更新和编辑容易(至少对我来说)。我想要的是能够优化if else分支并且不将它并入最终位的内容。我想要做这样的事情从调试版本中检测发布版本的最佳方法? .net

if(Debug.Mode){ 

<link rel="stylesheet" type="text/css" href="error.css" /> 
<link rel="stylesheet" type="text/css" href="login.css" /> 
<link rel="stylesheet" type="text/css" href="menu.css" /> 
<link rel="stylesheet" type="text/css" href="page.css" /> 
} else { 
<link rel="stylesheet" type="text/css" href="site.css" /> 
} 

我有一个MSBuild任务,将结合所有的CSS文件,尽量减少他们和所有的好东西。我只需要知道是否有办法删除最后一位中的if else分支。

+0

同类者问题在#1,一个问题,和很多很多不同的答案: http://*.com/questions/654450/programatically-detecting-release-debug-mode-net http://*.com/questions/798971/how-to-idenfiy-if-the-dll-is-debug-or-release-build-in-net http://*.com/questions/194616/how-to-tell-if-net-app-was-compiled-in-debug-or-release-mode http://*.com/questions/50900/best-way-to-detect- a-release-build-from-a-debug-build-net http://*.com/questions/890459/asp-net-release-build-vs-debug-build – Kiquenet 2011-02-03 19:51:18

+0

请参阅我的帖子: [如何判断程序集是调试还是发布](http: //dave-black.blogspot.com/2011/12/how-to-tell-if-assembly-is-debug-or.html)和 [http://*.com/questions/798971/how-to -idenfiy-如果最DLL-是调试 - 或释放积聚在网/ 5316565#5316565](http://*.com/questions/798971/how-to-idenfiy-if-the- dll-is-debug-or-release-build-in-net/5316565) – 2012-06-21 13:39:17

具体来说,像这样在C#:

#if (DEBUG) 
    Debug Stuff 
#endif 

C#有以下预处理器指令:

#if 
#else 
#elif // Else If 
#endif 
#define 
#undef // Undefine 
#warning // Causes the preprocessor to fire warning 
#error // Causes the preprocessor to fire a fatal error 
#line // Lets the preprocessor know where this source line came from 
#region // Codefolding 
#endregion 

我应该使用谷歌。

#if DEBUG 
    Console.WriteLine("Debug mode.") 
#else 
    Console.WriteLine("Release mode.") 
#endif 

确保选择“配置设置” - >“建立”,“在项目属性定义DEBUG 常数”被选中。

编译器常量。我不记得了C#语法,但是这是我如何做到这一点在VB:

#If CONFIG = "Debug" Then 
    'do somtehing 
#Else 
    'do something else 
#EndIf 

if (System.Diagnostics.Debugger.IsAttached) 
    { 
      // Do this 
    } 
    else 
    { 
      // Do that 
    } 
+5

这告诉你(在运行时)是否连接了调试器,但是如果程序集是DEBUG(vs RELEASE)构建,则不会。 – AlfredBr 2012-10-24 17:17:37

你可以尝试使用

HttpContext.Current.IsDebuggingEnabled 

它由配置中的节点控制。在我看来,这是比条件编译更好的解决方案。

但是,如果你想能够基于compilatino控制,我认为你可以使用ConditionalAttribute

问候,