Static value in web.config

I have a project on two servers. 1 test server with connection to testDB and one on a real server with a real database.

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

What I would like to do is the ability to set a value in the web.config file, bool, which can then be read by code. This bool would be true if the application is in test mode. I would install it manually, the project would then read it, and when it is true, the letters that the application sends will then be stored internally, so people don’t actually receive mail. I did this earlier by setting the public static bool in global.asax , but in Asp.net MVC everything is embedded in one DLL, so I cannot change it on a deployed server in this case.

Is it possible? or would there be a good other solution?

+6
source share
6 answers

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

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

Accessed via WebConfigurationManager

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

However

If you are interested in not sending emails during testing, you can use web.config to configure .NET to upload emails to a local directory rather than sending them to a 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 SMTP mail server settings.

+17
source

Yes, you can:

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

You can get it 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 usually use Enum for my application keys so that they are strictly typed, and this makes them faster and more convenient to search, rather than copying through web.config

+3
source

Why aren't you using appSettings ?

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

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

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

  #if (DEBUG) //Debug #else //Live #endif 
+1
source

ASP.Net WebDeploy allows you to automatically convert your web.config based on where you are deploying. for example, it can send one connection string when deployed to a test server and another connection string when deployed to a real server.

http://www.iis.net/download/WebDeploy

0
source

If you are using Visual Studio 2010, I would recommend using Transformation files to convert web.config. The more differences you have, the more this will help you.

High Level Steps:

  • Create a new build configuration through configuration manager
  • Set values ​​in each new configuration file
  • Assembly and publication
  • Ready and no need to remember changes in several configuration files (development, creation, release, etc.).

http://msdn.microsoft.com/en-us/library/dd465318.aspx

0
source

Source: https://habr.com/ru/post/887709/


All Articles