Problem
You have custom configuration information for your application that youwould like to store in its web.config
file.
Solution
Modify the web.config
file for your applicationby adding an <appSettings>
section tocontain the custom configuration settings.
Locate the
web.config
file in the root directoryof your application (or create one if it does not already exist).Add an
<appSettings>
element.Add
<add>
child elements along withkey
/value
pairs to the<appSettings>
element as required.In the code-behind class for your ASP.NET page, use the .NET languageof your choice to access the
<appSettings>
key/value collection through theConfigurationSettings
object.
Example 9-1 through Example 9-4 show the sample code we’vewritten to implement this solution. Example 9-1shows our web.config
file with somekey
/value
pairs. Example 9-2 shows the .aspx
file fora web form that displays the configuration settings. Example 9-3 (VB) and Example 9-4 (C#)show the code-behind class that accesses the configuration settingsusing the ConfigurationSettings
object.
Discussion
ASP.NET lets you add and then access configuration informationspecific to your application to the web.config
file by means of a special <appSettings>
element. You can add application configuration information by addingan <add>
child element for each parameter,setting the key
attribute to the name of theconfiguration parameter, and then setting thevalue
attribute to ...
Get ASP.NET Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.