static value in web.config

10,922

Solution 1

As the others have said this is what the appSettings section of your web.config is for

<configuration>
  <appSettings>
    <add key="isInTestMode" value="true"/>
  </appSettings>
  ...
</configuration>

Which can then be accessed using the WebConfigurationManager

bool isInTestMode = Boolean.Parse(WebConfigurationManager.AppSettings["isInTestMode"]);

However

If you only interested in not sending emails when testing, then you can use the web.config to configure .NET to dump the emails to a local directory rather than sender them to the mail server

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\MailDump\" />
      <network host="localhost"/>
    </smtp>
  </mailSettings>
  ...
</system.net>

This will work if your code does not override the default mail SMTP server settings.

Solution 2

Yes you can:

<configuration>
  <appSettings>
    <add key="TestingMode" value="True" />
  </appSettings>
   ...
</configuration>

You can get it out using something like this:

static public String GetWebConfigKey(string appSettingsKey)
    {
        String value = "";

        System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();

        try
        {
            value = asr.GetValue(appSettingsKey, System.Type.GetType("System.String")).ToString();
        }
        catch (KeyNotFoundException knfe)
        {
            throw new KeyNotFoundException("KeyNotFoundException occured in UtilityLibrary.WebConfig.getWebConfigKey" + knfe.Message);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return value;
    }

I typically use an Enum for my app keys to keep them strongly typed and it makes them quicker and easier to lookup rather than digging through the web.config

Solution 3

Why don't you use appSettings?

<configuration>
    <appSettings>
        <add key="myValue" value="true"/>
    </appSettings>
        ....

Solution 4

You can use your Web.Config to do this, using appSetting (ConfigurationManager.AppSetting["Key"])

Or, if yuor app is running in debug mode on the test server you can do this,

 #if (DEBUG)

 //Debug

#else

  //Live

#endif
Share:
10,922
Stefanvds
Author by

Stefanvds

ASP.Net, MVC and jQuery developer

Updated on July 01, 2022

Comments

  • Stefanvds
    Stefanvds almost 2 years

    I have a project running on 2 servers. 1 testserver with a connection to a testDB, and one on the real server, with real DB.

    The only different thing in each of the running instances of this project is the web.config.

    What i'd like to do is have the possibility to set a value in web.config, a bool, which then could be read by the code. This bool would be true if the app is in testing mode. i'd set it manually, the project then would read it out, and when it's true, the mails the app would send, then would be kept internal, so people dont actually get mail. I did this before with setting a public static bool in global.asax but in Asp.net MVC everything is built into one DLL, so i cant change it on the deployed server in that case.

    Is this possible? or would there be a nice other solution?

  • Martas
    Martas almost 7 years
    That catching the general Exception is bad example, how not to catch exceptions. What is its benefit? You have exception anyway, only you know less about it ...