Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Managing User Settings in the Web.Config File


Managing User Settings in the Web.Config File

Custom configuration data for ASP.NET applications is normally stored in the <appSettings> section of the web.config file and read from there using the AppSettings object. The following code snippet shows a very simple web.config file that contains the connection string of the application:

<configuration>
<appSettings>
   <add key="ConnectionString" value="..." />
</appSettings>
</configuration>

The syntax of the <appSettings> section is defined as follows:

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

The <add> element adds a new setting to the internal collection. This new setting has a value and is identified by a unique key. A bit trickier is the role of the <remove> and <clear> elements. In ASP.NET, configuration is hierarchical by nature and allows you to define settings at various levels—machine, root directory, single directories. The overall set of properties that apply to a particular page is built summing up all the settings found in the various web.config files found in the path from the root down to the page location. In light of this, the <remove> key is aimed at removing a particular item from the collection of settings. The setting is identified using the key. Finally, the <clear> element clears all the settings that have previously been defined in the section by upper-level configuration files.

The connection string is an example of the typical application-specific information you might want to store in web.config. You can read it back using the following code:

string connStr = ConfigurationSettings.AppSettings["ConnectionString"];

The AppSettings property of the class is a read-only NameValueCollection object designed to get the information stored in the <appSettings> section. The use of a NameValueCollection object stems from the following section declaration in machine.config file:

<section name="appSettings" 
type="System.Configuration.NameValueFileSectionHandler, System, ..."/>

The class responsible for reading the application settings is a section handler-NameValueFileSectionHandler-that specifically interprets and packages the read information as name/value pairs. If this scheme does not meet your needs, the most viable workaround is writing your own section handler. The MSDN documentation mentions the NameValueSectionHandler class as the default reader of application configuration settings. If you snoop into the machine.config file, though, you'll see that the registered section handler is the NameValueFileSectionHandler class. What's up?

The MSDN documentation doesn't provide further information about the NameValueFileSectionHandler class; it notes only that the class is intended to be used only by the .NET Framework. The NameValueFileSectionHandler class is actually a wrapper for the NameValueSectionHandler class, and provides an extra, although undocumented, feature. In particular, the NameValueFileSectionHandler section handler also allows the application settings to be stored in a separate file in accordance with the following syntax:

<configuration>
		<appSettings file="myfile.config" /> 
</configuration>

The contents of the file pointed to by the file attribute is read as if it were an <appSettings> section in the configuration file. Note that the root element of the file must match <appSettings>.

More in general, you can use the NameValueFileSectionHandler class wherever a NameValueSectionHandler is acceptable. Using external files is advantageous because any changes, although effective for the application, does not touch web.config and does not cause pages to recompile.

The NameValueFileSectionHandler object processes the contents of the embedded file using the NameValueSectionHandler class. If no file is embedded in the <appSettings> section but the default documented schema is used, the two section handlers are functionally equivalent.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Magazine and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.