In this tutorial we will add custom
configuration settings to the web configuration file easily by taking
advantage of the appSettings section. This section contains a list of
key and value pairs. Here is an example of a web.config file the
contains a two Messages; the first will retrieve the values
programmatically from the appSettings section and the second will
declaratively retrieve the values.
This tutorial will demonstrate how to add custom configuration settings to the web.config file.
<configuration>
<appSettings>
<add key=”Hello!” value=”We are retrieving values programmatically from appSettings!” />
<add key=”Wait! Check this out!” value=”We are retrieving values
declaratively from appSettings />
</appSettings>
</configuration>
|
Notice how we have two explicit messages describing two completely
different ways to add custom configuration settings to your web.config
file.
The first way we retrieved the values from the appSettings is
programmatically by placing this statement in the code-behind. They are
received programmatically from the WebConfigurationManager.AppSettings
property.
Protected void Page_Load(object sender, EventArgs e)
{
lblHello.Text = WebConfigurationManager.AppSettings[“Hello”];
}
|
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
There other way we can retrieve the same values is declaratively.
Now this value is retrieved by assigning it to a label control. The
System.Web.Configuration namespace must be imported before you can use
the WebConfigurationManager class.
<asp:Label
Id=”lblHello”
Runat=”server” />
<hr />
<asp:literal
Id=”ltlWait”
Text=”<%$ appSettings: Wait! Check this out! %>”
Runat=”server” />
|
Placing Configuration Settings in an External File
Do you want to place particular configuration sections in an
external file? The first way you can make a configuration file more
manageable could be by dividing it into multiple files. Also, when you
place configuration information in a separate file, you can prevent
application restarts when you change a configuration setting.
Here is an example of the web.config file using the configSource attribute in its <appSettings> element.
<configuration>
<appsettings configSource=”appSettings.config” />
</configuration>
|
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
All configuration elements include a configSource attribute.
Notably, you may also assign a path to a file as the value of the
configSource attribute.
Here is an example of when the appSettings are stored in an external file.
<appSettings>
<add key=”message” value=”Wait! Check this Offer Out!” />
</appSettings>
|
Typically, modifying any web configuration files result in your
ASP.NET application restarting. Although, the appSettings section is
declared in the Machine.config file with a
restartOnExternalChanges=”false” attribute. This is the attribute that
will prohibit your application from restarting when a change is made to
the appSettings section within an external configuration file. However,
if you modify this file below, your application won’t start.
<configuration>
<appsettings configSource=”appSettings.config” />
</configuration>
|
Here is how to directly access a property which you know the name of:
Given the following snippet in your web.config:
<!-- put this right after </configSections> -->
<appSettings>
<add key="AdminEmail" value="administrator@clientintellect.com"/>
</appSettings>
<!-- this should be just before <connectionStrings> -->
|
Then you can access it like this:
C#:
string AdminEmail = ConfigurationManager.AppSettings["AdminEmail"];