Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set different global variable values for release and debug mode C#

I'm developing an ASP.NET MVC web application using C#. I have a set a global variables, for example:

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

I want to set them with different values for release and debug mode. Could anyone give a suggestion on how to do this?

like image 419
Dean Avatar asked Oct 15 '25 16:10

Dean


2 Answers

You can use preprocessors directives for different debug/release values.

#if DEBUG
   public const string RootUrl = "http://url/";
#else
   public const string RootUrl = "http://another/";
#endif
like image 105
Vsevolod Goloviznin Avatar answered Oct 18 '25 07:10

Vsevolod Goloviznin


Although this is not direct answer, your question seems to be more configuration-related and I recomend to use web.config configuration for distinct environments. Look at your solution:

web.config.debug and web.config.release

Moreover, storing settings in external file is much better than embeding them into application's binaries. What if one of the URL's changes? You will have to recompile and deploy new version of application.

The simplest way is to use appSettings section in the web.config:

<configuration>
  <appSettings>
    <add key="GlobalValue1" value="1" />
  </appSettings>
</configuration>
like image 39
Konrad Kokosa Avatar answered Oct 18 '25 08:10

Konrad Kokosa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!