我复制并粘贴了Example on 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;
}
}
}
}
答案 0 :(得分: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; }
}
}
你准备好了!