O'Reilly logo

ASP.NET 2.0 Cookbook, 2nd Edition by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.2. Adding Custom Application Settings in web.config

Problem

You have custom configuration information for your application that you would like to store in its web.config file.

Solution

Modify the web.config file for your application by adding an <appSettings> element to contain the custom configuration settings:

  1. Locate the web.config file in the root directory of your application (or create one if it does not already exist).

  2. Add an <appSettings> element.

  3. Add <add> child elements along with key/value pairs to the <appSettings> element as required.

  4. In the code-behind class for your ASP.NET page, use the .NET language of your choice to access the <appSettings> key/value collection through the ConfigurationManager object.

Examples 12-1, 12-2, 12-3 through 12-4 show the sample code we’ve written to implement this solution. Example 12-1 shows our web.config file with some key/value pairs. Example 12-2 shows the .aspx file for a web form that displays the configuration settings. Examples 12-3 (VB) and 12-4 (C#) show the code-behind class that accesses the configuration settings using the ConfigurationManager object.

Discussion

ASP.NET lets you add and then access configuration information specific to your application to the web.config file by means of a special <appSettings> element. You can add application configuration information by adding an <add> child element for each parameter, setting the key attribute to the name of the configuration parameter, and setting the value attribute to the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required