Mega Code Archive

 
Categories / ASP.Net Tutorial / Configuration
 

Creating a Configuration Element Collection

File: App_Code\ShoppingCartSection.cs using System; using System.Configuration; namespace MyNamespace {     public class ShoppingCartSection : ConfigurationSection     {         [ConfigurationProperty("maximumItems", DefaultValue = 100, IsRequired = true)]         public int MaximumItems         {             get { return (int)this["maximumItems"]; }             set { this["maximumItems"] = value; }         }         [ConfigurationProperty("defaultProvider")]         public string DefaultProvider         {             get { return (string)this["defaultProvider"]; }             set { this["defaultProvider"] = value; }         }         [ConfigurationProperty("providers", IsDefaultCollection = false)]         public ProviderSettingsCollection Providers         {             get { return (ProviderSettingsCollection)this["providers"]; }         }         public ShoppingCartSection(int maximumItems, string defaultProvider)         {             this.MaximumItems = maximumItems;             this.DefaultProvider = defaultProvider;         }     } } File: Web.config <configuration>   <configSections>     <sectionGroup name="system.web">       <section         name="shoppingCart"         type="MyNamespace.ShoppingCartSection"         allowLocation="true"         allowDefinition="Everywhere" />     </sectionGroup> </configSections> <system.web>   <shoppingCart     maximumItems="50"     defaultProvider="SqlShoppingCartProvider">     <providers>       <add         name="SqlShoppingCartProvider"         type="MyNamespace.SqlShoppingCartProvider" />       <add         name="XmlShoppingCartProvider"         type="MyNamespace.XmlShoppingCartProvider" />     </providers>   </shoppingCart> </system.web> </configuration>