icrosoft has greatly enhanced the management tasks for your .NET 2.0 applications by providing a set of configuration APIs that you can use to programmatically access, modify, and save the configuration of an application using minimal code. This article explores the new and enhanced configuration API that ships with .NET Framework 2.0 by providing examples on how to use them from an ASP.NET Web application.
Introducing the ConfigurationManager Class
One important new configuration-related class is the ConfigurationManager class, which provides seamless programmatic access to configuration files and configuration sections. Using the methods and properties of this class, you can open configuration files and retrieve configuration sections from within your application. The ConfigurationMangager class's major functionality falls into three categories.
- Quick access to the appSettings and connectionStrings sections of the current application's default configuration file, which you access through properties such as AppSettings and ConnectionStrings.
- Access to a specific configuration section from the current application's default configuration file, using methods such as GetSection, GetWebAppSection, and RefreshSection.
- The ability to open different types of configuration files such as <AppName>.exe.config, machine.config, and web.config so you can update them programmatically through methods such as OpenMachineConfiguration, OpenMappedMachineConfiguration, OpenExeConfiguration, OpenMappedExeConfiguration, OpenWebConfiguration, and OpenMappedWebConfiguration.
You'll see examples of all of these ConfigurationManager class capabilities. Table 1 shows the most important methods and properties of the class.
Table 1. ConfigurationManager Properties and Methods: The table shows some important properties and methods of the Configuration Manager class.
|Author's Note: For all the examples shown in this article, the ASP.NET process account needs sufficient permissions to be able to read and write into the specified configuration file. For simplicity, to run the samples, enable Integrated Windows authentication through IIS and enable impersonation at the Web site level using the following configuration settings under the <system.web> element in the web.config file.|
Retrieving Connection Strings
This property gets the appropriate data from the <appSettings> section of the current application's configuration file.
This property gets the appropriate data from the <ConnectionStrings> section of the current application's configuration file.
This property opens the specified client configuration file as a configuration object.
This property opens the specified machine configuration file as a configuration object.
This property opens the specified Web application's configuration file as a configuration object.
This method allows you to refresh the named configuration section so that it is retrieved from the configuration file next time.
One of the most common questions ASP.NET developers have is how and where to store connection strings. .NET 2.0 introduces a new configuration section called <connectionStrings>
that lets you store connection strings securely in a .NET application. This section will show you how to read the connection string that is stored inside the <connectionStrings>
section in a web.config
First, you need to prepare your web.config file
. For example, here's a portion of a web.config
that contains a <connectionString>
The following code retrieves that connection string from the web.config
file using the ConnectionStrings
property of the ConfigurationManager class. Then it simply displays the retrieved connection string in the browser using the Response.Write
statement. Here's the code:
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat=server language=C# >
public void Page_Load(object source,
<title>Retrieving Connection Strings</title>
<form id="form1" runat="server">