Accessing and saving custom settings from web.config in ASP.NET
We usually save our application settings through key and value in appSettings element of web.config file. But accessing the attributes in coding is cumbersome. The string value is needed to enter everytime and then typecasting is need to be done.
Example:
web.config setting For accessing the values bool isTwitterEnabled = Convert.ToBoolean(System.Configuration.ConfigurationManager. AppSettings["enableTwitter"]);
enableTwitter is needed to entered manually without any intellisense and then typecasting is done.
If we have lot of values in appSettings then we have to mention the key name each time whenever we want to access.
The other way is, we can create our own XML element section in web.config along with class which will map to XML element. Through class we can access there values.
The process to implement are as follows:
1. Create XML element in web.config. The names you can provide according to your convenience.
2. Register above element in configSections of web.config and please note that configSections should be the first child of configuration element in web.config.
name will by the XML element that we have mentioned in 1st process and type will be the class for the element.
3. Create the class for accessing the elements value for above.
/// /// Class to map with web.config ApplicationCustomConfiguration /// public class AppConfiguration : ConfigurationSection { /// /// The property for twitter element in web.config /// [ConfigurationProperty("twitter", IsRequired = true)] public TwitterElement Twitter { get { return (TwitterElement)this["twitter"]; } } } /// /// The class for twitter element /// public class TwitterElement : ConfigurationElement { /// /// property for consumerKey attribute in twitter element /// [ConfigurationProperty("consumerKey")] public string ConsumerKey { get { return (string)this["consumerKey"]; } set { this["consumerKey"] = value; } } /// /// property for consumerSecretKey attribute in twitter element /// [ConfigurationProperty("consumerSecretKey")] public string ConsumerSecret { get { return (string)this["consumerSecretKey"]; } set { this["consumerSecretKey"] = value; } } /// /// property for enableTwitter attribute in twitter element /// [ConfigurationProperty("enableTwitter", DefaultValue = false)] public bool IsTwitterEnabled { get { return (bool)this["enableTwitter"]; } set { this["enableTwitter"] = value; } } /// /// property for userName attribute in twitter element /// [ConfigurationProperty("userName")] public string UserName { get { return (string)this["userName"]; } set { this["userName"] = value; } } /// /// Overrided method for allowing to change the settings /// /// public override bool IsReadOnly() { return false; } }
AppConfiguration class is the root element for ApplicationCustomConfiguration in web.config and this is derived from
System.Configuration.ConfigurationSection abstract class.
TwitterElement is the class for element under ApplicationCustomConfiguration in web.config and this is derived from System.Configuration.ConfigurationElement abstract class.
The typecasting is done in properties itself.
In some configuration setting of property you can find IsRequired. This means the property name should be present in web.config.
Along with IsRequired you can also provide DefaultValue. If the value is not set in web.config then the value that we provide in DefaultValue is automatically taken.
We have overrided the method IsReadOnly() to all allow the editing of elements in web.config.
4. Now, we are all set to use the our custom settings
//Create instance of AppConfiguration class to our specified section in web.config public static AppConfiguration settings = ((AppConfiguration)System.Web.Configuration. WebConfigurationManager.GetSection("ApplicationCustomConfiguration")); //Use properties to get the setting from web.config bool isTwitterEnabled = settings.Twitter.IsTwitterEnabled;
If we want to save something from code behind then we have to follow this procedure:
//Create configuration object from current web application System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); //Retrieve the specified configuration section AppConfiguration section = (AppConfiguration)config.GetSection("ApplicationCustomConfiguration"); //Set the user name section.Twitter.UserName = "User name specified"; //Save the configuration config.Save();
Some other links to get more information on custom settings:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationelement.aspx
http://www.4guysfromrolla.com/articles/032807-1.aspx
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx