How to read application settings from the Web.config file of an ASP.NET Website

There are many situations, in the development of an ASP.NET application, when particular values must be made visible from different Web Forms or code routines. These values can represent:

–    global values or keys associated with an application setting;
–    connections strings for databases
–    passwords and hash values
–    URLs for Web Services

The web.config file is an XML file that holds configuration information for the ASP.NET application. This file can be placed in the root directory of the ASP.NET site or in any other application subfolder. Among the configuration sections of the file, the application settings can be stored in the <appSettings> section using <add key=“” value=“”/> elements.

In order to describe how to read application settings, it is considered the next web.config file



	
		
	
	
	
		
		
		
		
			
			
		
	
	
	
	
	

The advantage of using web.config file for storing application settings:
–    application settings are managed from a single place, the web.config file;
–    application settings are accessible from any Web Form;
–    being stored in an XML file format, the settings are accessible from other applications, like legacy ASP Web applications;
–    application settings are protected by the web.config file (if it is possible or the application logic allows this, it is better not to store sensitive data in clear in the configuration file) by using Windows security settings to limit who can read it.

For accessing key values from the <appSettings> section of the web.config file, we can use the AppSettings property of the System.Configuration.ConfigurationSettings class.
In the Web Form code file or in other class, we can define a static function

    protected static string GetApplicationSettings(string sKey)
    {
        string sValue = null;
        System.Configuration.Configuration rootWebConfig =
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/WebSiteName");
        //check if the AppSettings section has items
        if(rootWebConfig.AppSettings.Settings.Count > 0)
        {
            sValue = rootWebConfig.AppSettings.Settings[sKey].Value;
        }
        return sValue;
    }

The way the function is used is:

string sAppSettingsValue = "Copyright: " + 
                 UtilityClass.GetApplicationSettings("Copyright");

Between using the index or named for of the AppSettings property is better to use the second approach because:
–    if the key value is missing, the property returns the null string;
–    if the <appSettings> section of the web.config file is missing, the property returns null string;
–    for the index form of the AppSettings property, if the property or the <appSettings> is not present then it is thrown an Index was out of range exception

This solution is considered deprecated by Microsoft, since .NET 2.0, and it is suggested to use the System.Configuration.Configuration class

    protected static string GetApplicationSettings(string sKey)
    {
        string sValue = null;
        System.Configuration.Configuration rootWebConfig =
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/WebSiteName");
        //check if the AppSettings section has items
        if(rootWebConfig.AppSettings.Settings.Count > 0)
        {
            sValue = rootWebConfig.AppSettings.Settings[sKey].Value;
        }
        return sValue;
    }

If the System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null) function it is called with a null value then it is opened the server root web.config file (something like C:\Windows\Microsoft.NET\Framework\v2.0.50727\Config\web.config ), and not the one from the current Web Site.

Another solution, based on the the System.Configuration.Configuration class, is not to use OpenWebConfiguration() function and to directly access the section of the Web Site web.config, like that:

    protected static string GetApplicationSettings(string sKey)
    {
        string sValue = null;
        //check if the AppSettings section has items
        if(System.Web.Configuration.WebConfigurationManager.AppSettings.Count > 0)
        {
            sValue = System.Web.Configuration.WebConfigurationManager.AppSettings[sKey];
        }
        return sValue;
    }

Other resources on this topic can be found on the Microsoft MSDN site, at