2014-07-02 47 views
1

下午,如何在app.config中创建一个自定义标签

经过四处寻找,发现很少,我想我会发布我想回答的问题。

正如问题所述,我希望在app.config文件中有一个自定义标记。

<appSettings> 
     <add key="exam" value="pp"/> 
     <add key="exam" value="ss" /> 
    </appSettings> 

<!-- This is the custom tag I want to have --!> 
    <WebPoints> 
     <host name="Main" value="URL" port="80" sslPort="633"></host> 
    </WebPoints> 

当然,当我运行代码时,它抱怨配置没有正确初始化。

我收集了我想要的东西,它比我们想要的要多得多,但我想知道这里有多少工作。

感谢

回答

2

要建立在web.config中的自定义标签可以使用Custom Configuration sections

C#代码

using System; 
using System.Collections; 
using System.Text; 
using System.Configuration; 
using System.Xml; 

namespace Samples.AspNet 
{ 
    public class PageAppearanceSection : ConfigurationSection 
    { 
     // Create a "remoteOnly" attribute. 
     [ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)] 
     public Boolean RemoteOnly 
     { 
      get 
      { 
       return (Boolean)this["remoteOnly"]; 
      } 
      set 
      { 
       this["remoteOnly"] = value; 
      } 
     } 

     // Create a "font" element. 
     [ConfigurationProperty("font")] 
     public FontElement Font 
     { 
      get 
      { 
       return (FontElement)this["font"]; } 
      set 
      { this["font"] = value; } 
     } 

     // Create a "color element." 
     [ConfigurationProperty("color")] 
     public ColorElement Color 
     { 
      get 
      { 
       return (ColorElement)this["color"]; 
      } 
      set 
      { this["color"] = value; } 
     } 
    } 

    // Define the "font" element 
    // with "name" and "size" attributes. 
    public class FontElement : ConfigurationElement 
    { 
     [ConfigurationProperty("name", DefaultValue="Arial", IsRequired = true)] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] 
     public String Name 
     { 
      get 
      { 
       return (String)this["name"]; 
      } 
      set 
      { 
       this["name"] = value; 
      } 
     } 

     [ConfigurationProperty("size", DefaultValue = "12", IsRequired = false)] 
     [IntegerValidator(ExcludeRange = false, MaxValue = 24, MinValue = 6)] 
     public int Size 
     { 
      get 
      { return (int)this["size"]; } 
      set 
      { this["size"] = value; } 
     } 
    } 

    // Define the "color" element 
    // with "background" and "foreground" attributes. 
    public class ColorElement : ConfigurationElement 
    { 
     [ConfigurationProperty("background", DefaultValue = "FFFFFF", IsRequired = true)] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)] 
     public String Background 
     { 
      get 
      { 
       return (String)this["background"]; 
      } 
      set 
      { 
       this["background"] = value; 
      } 
     } 

     [ConfigurationProperty("foreground", DefaultValue = "000000", IsRequired = true)] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)] 
     public String Foreground 
     { 
      get 
      { 
       return (String)this["foreground"]; 
      } 
      set 
      { 
       this["foreground"] = value; 
      } 
     } 

    } 

} 

的web.config

<configuration> 
<!-- Configuration section-handler declaration area. --> 
    <configSections> 
    <sectionGroup name="pageAppearanceGroup"> 
     <section 
     name="pageAppearance" 
     type="Samples.AspNet.PageAppearanceSection" 
     allowLocation="true" 
     allowDefinition="Everywhere" 
     /> 
    </sectionGroup> 
     <!-- Other <section> and <sectionGroup> elements. --> 
    </configSections> 

    <!-- Configuration section settings area. --> 

</configuration> 
相关问题