Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static value in web.config

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?

like image 481
Stefanvds Avatar asked May 10 '11 11:05

Stefanvds


1 Answers

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.

like image 168
David Glenn Avatar answered Oct 09 '22 18:10

David Glenn