ConfigurationSection - StringValidator失败

时间:2013-01-14 05:44:15

标签: configuration

我想在自定义元素的ASP.NET web.config文件中存储三个值,所以这是我的web.config:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="mySection" type="Foobar.MySection,Foobar" />
    </configSections>

    <mySection baseUri="https://blargh" sid="123" key="abc" />

    <!-- etc, including <system.web> configuration -->
</configuration>

这是我的配置代码:

namespace Foobar {

public class MySection : ConfigurationSection {

    public MySection () {
    }

    [ConfigurationProperty("baseUri", IsRequired=true)]
    [StringValidator(MinLength=1)]
    public String BaseUri {
        get { return (String)this["baseUri"]; }
        set { this["baseUri"] = value; }
    }

    [ConfigurationProperty("sid", IsRequired=true)]
    [StringValidator(MinLength=1)]
    public String Sid {
        get { return (String)this["sid"]; }
        set { this["sid"] = value; }
    }

    [ConfigurationProperty("key", IsRequired=true)]
    [StringValidator(MinLength=1)]
    public String Key {
        get { return (String)this["key"]; }
        set { this["key"] = value; }
    }
}
}

我使用以下代码加载它:

MySection section = ConfigurationManager.GetSection("mySection") as MySection;
String x = section.BaseUri;

然而,当我在ASP.NET中运行我的代码时,我得到了这个例外:

[ArgumentException: The string must be at least 1 characters long.]
System.Configuration.StringValidator.Validate(Object value) +679298
System.Configuration.ConfigurationProperty.Validate(Object value) +41

[ConfigurationErrorsException: The value for the property 'baseUri' is not valid. The error is: The string must be at least 1 characters long.]
System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line) +278
System.Configuration.BaseConfigurationRecord.CreateSectionDefault(String configKey, Boolean getRuntimeObject, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object& result, Object& resultRuntimeObject) +59
System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) +1431
System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission) +56
System.Configuration.BaseConfigurationRecord.GetSection(String configKey) +8
System.Web.HttpContext.GetSection(String sectionName) +47
System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName) +39
System.Web.Configuration.HttpConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String configKey) +6
System.Configuration.ConfigurationManager.GetSection(String sectionName) +78
<my code that calls GetSection>

为什么在我的web.config中设置了正确格式化的值时,StringValidator会失败?我在俯瞰什么?

3 个答案:

答案 0 :(得分:4)

因为StringValidator是异常的原因我做了一些谷歌搜索,显然它是.NET Framework中的一个错误:拥有一个StringValidator的最小长度参数意味着它总是拒绝空字符串""属性的默认值,由框架设置。

有一些解决方法,但我无法证明花时间在它们上面,所以我删除了StringValidator属性,我的代码现在工作正常。

以下是我发现的问答:Why does StringValidator always fail for custom configuration section?

答案 1 :(得分:0)

明确指出

  

属性'baseUri'的值无效

我认为应该是

<mySection baseUri="https://blargh"  sid="123" key="abc" />

答案 2 :(得分:0)

StringValidator是根本问题,它可以通过以下任何一个解决:

  • 删除MinLength参数
  • 设置MinLength = 0
  • 删除StringValidator属性
  • 将DefaultValue添加到ConfigurationProperty属性

该属性的理想定义如下:

    [ConfigurationProperty("title", IsRequired = true, DefaultValue = "something")]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;’\"|\\"
      , MinLength = 1
      , MaxLength = 256)]
    public string Title
    {
        get { return this["title"] as string; }
        set { this["title"] = value; }
    }
相关问题