自定义配置部分 [英] Custom configuration section

查看:28
本文介绍了自定义配置部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我从

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

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

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

other than that all the code is the exact same. For some reason the source code from msdn only displays the default value. Can someone help me figure out why it will not read the config section please, Thank you.

<?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;
            }
        }

    }

}

推荐答案

关于你的情况,我调试了一下,发现哪里不对了.只需评论以下行:

About your case, I have debug it and found what is wrong. Just comment following line:

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

所以类 FontElement 应该是这样的:

So class FontElement should look something like this:

// 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; }
    }
}

你准备好了!

这篇关于自定义配置部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆