首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConfigurationSection - StringValidator失败

ConfigurationSection - StringValidator失败
EN

Stack Overflow用户
提问于 2013-01-14 13:44:15
回答 3查看 2.1K关注 0票数 2

我希望在自定义元素中的ASP.NET web.config文件中存储三个值,因此这是我的web.config:

代码语言:javascript
复制
<?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>

这是我的配置代码:

代码语言:javascript
复制
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; }
    }
}
}

我使用下面的代码加载它:

代码语言:javascript
复制
MySection section = ConfigurationManager.GetSection("mySection") as MySection;
String x = section.BaseUri;

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

代码语言:javascript
复制
[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会失败?我忽略了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-14 14:09:53

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

有一些变通方法,但是我不能证明在它们上面花费时间是合理的,所以我去掉了StringValidator属性,我的代码现在运行得很好。

下面是我找到的QA:Why does StringValidator always fail for custom configuration section?

票数 4
EN

Stack Overflow用户

发布于 2013-01-14 13:49:34

它清楚地说明了

属性'baseUri‘的值无效

我觉得应该是

代码语言:javascript
复制
<mySection baseUri="https://blargh"  sid="123" key="abc" />
票数 0
EN

Stack Overflow用户

发布于 2016-08-27 07:52:01

StringValidator是根本问题,可以通过以下任何一种方法来解决:

  • Removing MinLength argument
  • MinLength =0
  • removing StringValidator Attribute
  • adding DefaultValue to ConfigurationProperty Attribute

该属性的理想定义如下:

代码语言:javascript
复制
    [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; }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14313788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档