ASP.NET Tutorial/Configuration/New Section

Материал из .Net Framework эксперт
Версия от 14:57, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Creating a Configuration Element Collection

   <source lang="csharp">

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></source>


Creating Custom Configuration Sections by inheriting a new class from ConfigurationSection class

   <source lang="csharp">

File: App_Code\DesignSection.cs using System; using System.Configuration; using System.Drawing; namespace MyNamespace {

   public class DesignSection : ConfigurationSection
   {
       [ConfigurationProperty("backcolor", DefaultValue = "lightblue", IsRequired = true)]
       public Color BackColor
       {            get { return (Color)this["backcolor"]; }
           set { this["backcolor"] = value; }
       }
       [ConfigurationProperty("styleSheetUrl", DefaultValue = "~/styles/style.css", IsRequired = true)]
       [RegexStringValidator(".css$")]
       public string StyleSheetUrl
       {
           get { return (string)this["styleSheetUrl"]; }
           set { this["styleSheetUrl"] = value; }
       }
       public DesignSection(Color backcolor, string styleSheetUrl)
       {
           this.BackColor = backcolor;
           this.StyleSheetUrl = styleSheetUrl;
       }
       public DesignSection()
       {
       }
   }

} Register it in a configuration file. File: Web.config <configuration>

 <configSections>
   <sectionGroup name="system.web">
   <section
       name="design"
       type="MyNamespace.DesignSection"
       allowLocation="true"
       allowDefinition="Everywhere"/>
   </sectionGroup>
 </configSections>
 <system.web>
   <design
     backcolor="red"
     styleSheetUrl="~/styles/style.css"/>
 </system.web>

</configuration></source>


Using the custom configuration section to modify the page style and background color.

   <source lang="csharp">

<%@ Page Language="C#" %> <%@ Import Namespace="MyNamespace" %> <%@ Import Namespace="System.Web.Configuration" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   void Page_Load()
   {
       DesignSection section = (DesignSection)WebConfigurationManager. GetWebApplicationSection("system.web/design");
       htmlBody.Attributes["bgcolor"] = System.Drawing.ColorTranslator.ToHtml (section.BackColor);
       HtmlLink link = new HtmlLink();
       link.Href = section.StyleSheetUrl;
       link.Attributes.Add("rel", "stylesheet");
       link.Attributes.Add("type", "text/css");
       Page.Header.Controls.Add(link);
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Show Design Section</title>

</head> <body id="htmlBody" runat="server">

   <form id="form1" runat="server">

Custom Configuration Section Sample

   </form>

</body> </html> File: Web.config <configuration>

 <configSections>
   <sectionGroup name="system.web">
   <section
       name="design"
       type="MyNamespace.DesignSection"
       allowLocation="true"
       allowDefinition="Everywhere"/>
   </sectionGroup>
 </configSections>
 <system.web>
   <design
     backcolor="red"
     styleSheetUrl="~/styles/style.css"/>
 </system.web>

</configuration></source>