我正在使用C#开发ASP.NET MVC Web应用程序。我有一个全局变量集,例如:
public static class Global
{
public const string RootUrl = "http://url/";
}
我想为发布和调试模式设置不同的值。任何人都可以就如何做到这一点提出建议吗?
答案 0 :(得分:7)
您可以将preprocessors directives用于不同的调试/释放值。
#if DEBUG
public const string RootUrl = "http://url/";
#else
public const string RootUrl = "http://another/";
#endif
答案 1 :(得分:6)
虽然这不是直接答案,但您的问题似乎更多与配置相关,我建议对不同的环境使用web.config
配置。看看你的解决方案:
此外,将设置存储在外部文件中要比将它们嵌入到应用程序的二进制文件中要好得多。如果其中一个URL发生了变化怎么办?您将不得不重新编译和部署新版本的应用程序。
最简单的方法是在appSettings
中使用web.config
部分:
<configuration>
<appSettings>
<add key="GlobalValue1" value="1" />
</appSettings>
</configuration>
答案 2 :(得分:0)
我相信它是以下内容:
#if !DEBUG
// Your code here
#endif