1

我在 MSDN 上复制并粘贴了示例

我将部分声明从

<section 
    name="pageAppearance" 
    type="Samples.AspNet.PageAppearanceSection" 
    allowLocation="true" 
    allowDefinition="Everywhere"
  />

<section
    name="pageAppearance"
    type="Samples.AspNet.PageAppearanceSection, Samples.AspNet " />

除此之外,所有代码都是完全相同的。出于某种原因,来自 msdn 的源代码仅显示默认值。有人可以帮我弄清楚为什么它不会读取配置部分,谢谢。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <!-- Configuration section-handler declaration area. -->
    <configSections>
        <sectionGroup name="pageAppearanceGroup">
            <section
        name="pageAppearance"
        type="Samples.AspNet.PageAppearanceSection, Samples.AspNet "
                allowLocation="true"
                allowDefinition="Everywhere"/>
        </sectionGroup>
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <pageAppearanceGroup>
        <pageAppearance remoteOnly="true">
            <font name="TimesNewRoman" size="18"/>
            <color background="000000" foreground="FFFFFF"/>
        </pageAppearance>
    </pageAppearanceGroup>
</configuration>

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")]
        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")]
        [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;
            }
        }

    }

}
4

1 回答 1

1

关于您的情况,我已经对其进行了调试,并发现了问题所在。只需评论以下行:

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]

所以类 FontElement 应该是这样的:

// Define the "font" element 
// with "name" and "size" attributes. 
public class FontElement : ConfigurationElement
{

    [ConfigurationProperty("name")]
    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; }
    }
}

你准备好了!

于 2012-10-01T16:37:11.350 回答