You have custom configuration information for
your application that you
would like to store in its web.config
file.
Modify the web.config
file for your application
by adding an <appSettings>
section to
contain the custom configuration settings.
Locate the web.config
file in the root directory
of your application (or create one if it does not already exist).
Add an <appSettings>
element.
Add <add>
child elements along with
key
/value
pairs to the
<appSettings>
element as required.
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
ConfigurationSettings
object.
Example 9-1 through Example 9-4 show the sample code we’ve
written to implement this solution. Example 9-1
shows our web.config
file with some
key
/value
pairs. Example 9-2 shows the .aspx
file for
a 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 settings
using the ConfigurationSettings
object.
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 then setting the
value
attribute to the value of the configuration
parameter, as shown in Example 9-1.
Notice that the <appSettings>
element is not
a child of <system.web>
, like some of the
other web.config elements we
discuss in this chapter. Rather, it is a subsection all its own
within the <configuration>
section.
When your application is started, ASP.NET creates a
NameValueCollection
from the
key
/value
pairs in the
<appSettings>
section. You can access this
NameValueCollection
anywhere in your application
through the ConfigurationSettings
object. Any data
that can be represented as a string can be stored in the
<appSettings>
section, but anything other
than a string will need to be cast to the appropriate data type for
use in your application.
Recipe 9.6 for how to store an
application’s configuration information in its own
custom section in web.config
Example 9-1. Application settings in web.config
<?xml version="1.0" encoding="utf-8" ?> <configuration><appSettings>
<add key="defaultSortField" value="Title" />
<add key="defaultSortOrder" value="Ascending" />
<add key="defaultResultsPerPage" value="25" />
</appSettings>
</configuration>
Example 9-2. Accessing application settings in web.config (.aspx)
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="CH09GetAppSettingsVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH09GetAppSettingsVB"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Get Application Settings</title> <link rel="stylesheet" href="css/ASPNetCookbook.css"> </head> <body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0"> <form id="frmConfiguration" method="post" runat="server"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td align="center"> <img src="images/ASPNETCookbookHeading_blue.gif"> </td> </tr> <tr> <td class="dividerLine"> <img src="images/spacer.gif" height="6" border="0"></td> </tr> </table> <table width="90%" align="center" border="0"> <tr> <td><img src="images/spacer.gif" height="10" border="0"></td> </tr> <tr> <td align="center" class="PageHeading"> Application Configuration In web.config (VB) </td> </tr> <tr> <td><img src="images/spacer.gif" height="10" border="0"></td> </tr> <tr> <td align="center"> <table> <tr> <td class="LabelText">Sort Field: </td> <td class="LabelText"> <asp:Label ID="labSortField" Runat="server" /></td> </tr> <tr> <td class="LabelText">Sort Order: </td> <td class="LabelText"> <asp:Label ID="labSortOrder" Runat="server" /></td> </tr> <tr> <td class="LabelText">Number of Pages: </td> <td class="LabelText"> <asp:Label ID="labNumberOfPages" Runat="server" /></td> </tr> </table> </td> </tr> </table> </form> </body> </html>
Example 9-3. Accessing application settings in web.config (.vb)
Option Explicit On Option Strict On '----------------------------------------------------------------------------- ' ' Module Name: CH09GetAppSettingsVB.aspx.vb ' ' Description: This module provides the code behind for the ' CH09GetAppSettingsVB.aspx page ' '***************************************************************************** Imports System Imports System.Configuration Namespace ASPNetCookbook.VBExamples Public Class CH09GetAppSettingsVB Inherits System.Web.UI.Page 'controls on the form Protected labSortField As System.Web.UI.WebControls.Label Protected labSortOrder As System.Web.UI.WebControls.Label Protected labNumberOfPages As System.Web.UI.WebControls.Label '************************************************************************* ' ' ROUTINE: Page_Load ' ' DESCRIPTION: This routine provides the event handler for the page load ' event. It is responsible for initializing the controls ' on the page. '------------------------------------------------------------------------- Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim resultsPerPage As Integer'initialize labels on form from values in web.config
labSortField.Text = ConfigurationSettings.AppSettings("defaultSortField")
labSortOrder.Text = ConfigurationSettings.AppSettings("defaultSortOrder")
'get an integer value from web.config and do a little calculating
resultsPerPage = _
CInt(ConfigurationSettings.AppSettings("defaultResultsPerPage"))
labNumberOfPages.Text = Math.Ceiling(1234.0 / resultsPerPage).ToString( )
End Sub 'Page_Load End Class 'CH09GetAppSettingsVB End Namespace
Example 9-4. Accessing application settings in web.config (.cs)
//---------------------------------------------------------------------------- // // Module Name: CH09GetAppSettingsCS.aspx.cs // // Description: This module provides the code behind for the // CH09GetAppSettingsCS.aspx page // //**************************************************************************** using System; using System.Configuration; namespace ASPNetCookbook.CSExamples { public class CH09GetAppSettingsCS : System.Web.UI.Page { // controls on the form protected System.Web.UI.WebControls.Label labSortField; protected System.Web.UI.WebControls.Label labSortOrder; protected System.Web.UI.WebControls.Label labNumberOfPages; //************************************************************************ // // ROUTINE: Page_Load // // DESCRIPTION: This routine provides the event handler for the page // load event. It is responsible for initializing the // controls on the page. //------------------------------------------------------------------------ private void Page_Load(object sender, System.EventArgs e) { int resultsPerPage;// initialize labels on form from values in web.config
labSortField.Text =
ConfigurationSettings.AppSettings["defaultSortField"];
labSortOrder.Text =
ConfigurationSettings.AppSettings["defaultSortOrder"];
// get an integer value from web.config and do a little calculating
resultsPerPage =
Convert.ToInt32(ConfigurationSettings.AppSettings["defaultResultsPerPage"]);
labNumberOfPages.Text = Math.Ceiling(1234.0 / resultsPerPage).ToString( );
} // Page_Load } // CH09GetAppSettingsCS }