我希望在自定义元素中的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会失败?我忽略了什么?
发布于 2013-01-14 14:09:53
因为StringValidator是导致异常的原因,所以我做了一些谷歌搜索,显然这是.NET框架中的一个错误:拥有一个带有最小长度参数的StringValidator意味着它将始终拒绝该属性的空字符串""默认值,该值将由框架设置。
有一些变通方法,但是我不能证明在它们上面花费时间是合理的,所以我去掉了StringValidator属性,我的代码现在运行得很好。
下面是我找到的QA:Why does StringValidator always fail for custom configuration section?
发布于 2013-01-14 13:49:34
它清楚地说明了
属性'baseUri‘的值无效
我觉得应该是
<mySection baseUri="https://blargh" sid="123" key="abc" />发布于 2016-08-27 07:52:01
StringValidator是根本问题,可以通过以下任何一种方法来解决:
该属性的理想定义如下:
[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; }
}https://stackoverflow.com/questions/14313788
复制相似问题