【问题标题】:How to create a custom tag in app.config如何在 app.config 中创建自定义标签
【发布时间】:2014-07-02 14:00:33
【问题描述】:

下午,

在四处搜索并找到很少的内容之后,我想我会发布我想要回答的问题。

正如问题所述,我希望在 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>

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

我发现我想要的工作比应有的要多得多,但我想知道有多少工作。

谢谢

【问题讨论】:

    标签: c# .net xml config


    【解决方案1】:

    要在 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 = "~!@#$%^&*()[]{}/;'\"|\\", 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 = "~!@#$%^&*()[]{}/;'\"|\\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 = "~!@#$%^&*()[]{}/;'\"|\\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>
    

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 2012-01-24
      • 2013-01-22
      • 2014-09-03
      • 2011-11-21
      • 2012-02-29
      • 2017-08-20
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多