Microsoft ASP.NET Programming with Microsoft Visual Basic .NET Version 2003 Step by Step [Electronic resources]

G. Andrew Duthie

نسخه متنی -صفحه : 126/ 95
نمايش فراداده

Setting and Retrieving Custom Application Settings

The final configuration element is a special one because it allows you to configure your own custom application settings, which can then be easily retrieved from within your ASP.NET applications.

<appSettings>

The <appSettings> element allows you to create your own configuration settings as key/value pairs. You can use this configuration element to store configuration information such as DSNs for data access, and then retrieve them in your application using the following syntax:

DimDSNAsString
DSN=ConfigurationSettings.AppSettings("dsn")

This is assuming that you’ve stored the DSN using “dsn” as the key.

The <appSettings> element has the following syntax:

<appSettings>
<add
key="string"
value="string"/>
<clear/>
<remove
key="string"/>
</appSettings>

Table B-20 describes the <appSettings> element attributes in more detail.

Table B-20: <appSettings> Element Attributes

Element

Attribute

Description

<appSettings>

Creates key/value pairs that can be retrieved using ConfigurationSettings.AppSettings(key).

<add>

Adds a key/value pair.

key

Specifies the name of the key to use to retrieve the value.

value

Specifies the value to be returned.

<clear>

Removes all key/value pairs added (or inherited) by the current Web.config file.

<remove>

key

Removes the specified name/value pair added (or inherited) by the current Web.config file.