August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

ASP.NET Secrets, Part 3

  • March 26, 2003
  • By Karl Moore
  • Send Email »
  • More Articles »

The Best Place to Store Your Settings

Many Web developers end up storing important pieces of data—database connection strings, passwords, default settings—as troublesome constants. The problem with this is that they're difficult to edit: The slightest of changes means a complete recompile.

However, with ASP.NET, there is an easier way. It's likely you're well aware of the Web.config file, which stores settings for your applications, such as the session state timeout and authentication mode. It can also hold your own settings.

How? It's easy. Simply add your own personal settings to the Web.config file, like this:

<configuration>
...
<appSettings>
  <add key="serverName" value="diomedes" />
</appSettings>
...
<system.web>
...
</system.web>
</configuration>

Here, I've added a key called "serverName" containing a value of "diomedes". You can list as many values here as you like: just keep adding <add> elements. And how do you read the value in your code? Simply reference the AppSettings collection of the System.Configuration.ConfigurationSettings class, as so:

x = System.Configuration.ConfigurationSettings.AppSettings _
           ("serverName")

And don't forget: Web.config is just an XML file and easily editable without a recompile. You could even use a third-party control, such as the free Web.config Editor from Hunterstone, to simplify all your administration. Easy!





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel