如何为发布和调试模式C#设置不同的全局变量值

时间:2014-11-20 23:36:43

标签: c# visual-studio release

我正在使用C#开发ASP.NET MVC Web应用程序。我有一个全局变量集,例如:

public static class Global
{
    public const string RootUrl = "http://url/";
}

我想为发布和调试模式设置不同的值。任何人都可以就如何做到这一点提出建议吗?

3 个答案:

答案 0 :(得分:7)

您可以将preprocessors directives用于不同的调试/释放值。

#if DEBUG
   public const string RootUrl = "http://url/";
#else
   public const string RootUrl = "http://another/";
#endif

答案 1 :(得分:6)

虽然这不是直接答案,但您的问题似乎更多与配置相关,我建议对不同的环境使用web.config配置。看看你的解决方案:

web.config.debug and web.config.release

此外,将设置存储在外部文件中要比将它们嵌入到应用程序的二进制文件中要好得多。如果其中一个URL发生了变化怎么办?您将不得不重新编译和部署新版本的应用程序。

最简单的方法是在appSettings中使用web.config部分:

<configuration>
  <appSettings>
    <add key="GlobalValue1" value="1" />
  </appSettings>
</configuration>

答案 2 :(得分:0)

我相信它是以下内容:

#if !DEBUG
    // Your code here
#endif