Understanding best use of config files for Web Applications  
Author Message
Andrew Mercer





PostPosted: Visual C# General, Understanding best use of config files for Web Applications Top

Hi,

Web Applications use the web.config file however they do not seem to allow me to add extra parameters that I need. (Note: The extra parameters do not always relate to the normal items stored in this config file)

For example, I would like to add something similar to the following:

<myUniqueConfigSettings>

<add name="MyUniqueSettings" myUniqueSettingString="blah; blah;">

</myUniqueConfigSettings>

Is this possible Or do I have to create my own separate config file for such items and read from that

Thanks in advance

Andrew




Visual C#18  
 
 
TaylorMichaelL





PostPosted: Visual C# General, Understanding best use of config files for Web Applications Top

The web.config file works just like a normal application configuration file. In order to add new sections to the file you need to define a configuration section handler (ConfigurationSection in v2) and add it to the config file's section handler list. Your handler will then be called when you attempt to access it through ConfigurationManager. Describing this process is too long for the forums but MSDN has some example code and an article published a while back in MSDN Magazine also discusses the process. As a side note .NET ships with a couple common handlers for name-value pairs and dictionaries so look up IConfigurationSectionHandler for the pre-defined types.

If you just want to store a couple of simple values then you can use the pre-defined AppSettings section which allows for name-value pairs. For connection strings they should be stored in ConnectionStrings.

Michael Taylor - 12/7/06