2

我想在我的 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 会失败?我在看什么?

4

3 回答 3

4

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

有一些变通方法,但我无法证明在它们上花费时间是合理的,所以我去掉了StringValidator属性,我的代码现在可以正常工作了。

这是我发现的 QA:为什么自定义配置部分的 StringValidator 总是失败?

于 2013-01-14T06:09:53.077 回答
0

它明确指出

属性“baseUri”的值无效

我认为应该是

<mySection baseUri="https://blargh"  sid="123" key="abc" />
于 2013-01-14T05:49:34.053 回答
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; }
    }
于 2016-08-26T23:52:01.270 回答