2013-01-14 38 views
2

我想存儲在一個自定義元素我的ASP.NET web.config文件中三個值,所以這是我的web.config:的ConfigurationSection - StringValidator失敗

<?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

因爲StringValidator是的原因異常我做了一些Google搜索,顯然這是一個在.NET Framework中的錯誤:擁有一個StringValidator與最小長度argum ent意味着它會一直拒絕空的字符串""該屬性的默認值,該屬性將由框架設置。

有解決方法,但我沒有理由花時間在他們身上,所以我剝離了StringValidator屬性,我的代碼現在工作正常。

這是我發現的QA:Why does StringValidator always fail for custom configuration section?

0

它明確指出,

的財產「的基本URI」的值無效

我覺得應該是

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

對不起,那是一個錯字,當我抄我的web.config中的StackOverflow。我的實際web.config文件是格式良好的。 – Dai

+0

甚至,我沒有期待這樣的聲譽的人這樣的錯誤;) –

+0

我通過解決大量簡單的問題而不是幾個難題來獲得我的聲譽。這是我的祕密恥辱。 – Dai

0

的StringValidator是問題的根源,它可以由任何一個來解決:

  • 刪除MINLENGTH個說法
  • 設置的minLength = 0
  • 刪除StringValidator屬性
  • 將DefaultValue添加到ConfigurationProperty屬性

該屬性的理想的定義是這樣的:

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