2

カスタム要素の ASP.NET web.config ファイルに 3 つの値を保存したいので、これが私の 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 属性の削除
  • ConfigurationProperty 属性に DefaultValue を追加する

プロパティの理想的な定義は次のようになります。

    [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 に答える