The more flexible variant of the storing web application settings is the using of Web.config file.
Configuration information for ASP.NET resources is contained in a collection of configuration files, each named Web.config. Each configuration file contains a nested hierarchy of XML tags and subtags with attributes that specify the configuration settings. Because the tags must be well-formed XML, the tags, subtags, and attributes are case-sensitive. Tag names and attribute names are camel-cased, which means that the first character of a tag name is lowercase and the first letter of any subsequent concatenated words is uppercase. Attribute values are Pascal-case, which means that the first character is uppercase and the first letter of any subsequent concatenated words is uppercase. Exceptions are true and false, which are always lowercase.
All configuration information resides between the <configuration> and </configuration> root XML tags.
There is a special <appSettings> Element in Web.config, which can be used for storing custom application settings. This is a predefined configuration section provided by the .NET Framework.
The <appSettings> element stores custom application configuration information such as database connection strings, file paths, XML Web service URLs, or any information stored in an application's .ini file. The key/value pairs specified in the <appSettings> element can be accessed in code using the System.Configuration.ConfigurationSettings or System.Configuration.AppSettingsReader classes.
Review the following example:
Thus we can retrieve the values of our parameters by using the static methods of myConfigurator class and use these parameters in all places of our application.
Let's consider the advantages and disadvantages of using Web.config as a storage for web application parameters.
Advantages:
- Settings can be operatively changed
- No need to recompile whole application
Disadvantages:
- After changing of settings application restart occurs
- Additional system resources for file-based operations
- Anyone, who has access to Web.config file, can change settings