Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以在自定义ConfigurationSection上指定具有IntegerValidator属性的范围吗?_C#_Asp.net_Validation_Configurationsection - Fatal编程技术网

C# 我可以在自定义ConfigurationSection上指定具有IntegerValidator属性的范围吗?

C# 我可以在自定义ConfigurationSection上指定具有IntegerValidator属性的范围吗?,c#,asp.net,validation,configurationsection,C#,Asp.net,Validation,Configurationsection,我有一个包含以下内容的类: 然后,我的.config文件中有以下内容: <configSections> <section name="TestingComponentSettings" type="DummyConsole.TestingComponentSettings, DummyConsole"/> </configSections> <TestingComponentSettings waitForTimeSecon

我有一个包含以下内容的类:

然后,我的.config文件中有以下内容:

<configSections>
  <section name="TestingComponentSettings" 
           type="DummyConsole.TestingComponentSettings, DummyConsole"/>
</configSections>
<TestingComponentSettings waitForTimeSeconds="20" loginPage="myPage" />
未处理配置错误异常

属性“waitForTimeSeconds”的值无效。错误是:该值必须在1-100范围内

如果我将
IntegerValidator
更改为ExcludeRage=true,我(显然)会得到:

未处理配置错误异常

属性“waitForTimeSeconds”的值无效。错误是:该值不能在1-100范围内

然后,如果我将.config中属性的值更改为大于100的数字,则它可以工作

如果我将验证器更改为只有
MaxValue
为100,那么它可以工作,但也会接受-1的值

是否可以将
IntegerValidatorAttribute
用于这样的范围

编辑以添加

已确认为。

如前所述,MS已更新连接问题:

报告的问题是由于配置系统如何处理验证器的一个怪癖。每个数字配置属性都有一个默认值-即使未指定默认值。如果未指定默认值,则使用值0。在本例中,configuration属性的默认值不在整数验证器指定的有效范围内。因此,配置解析总是失败

要解决此问题,请更改配置属性定义,使其包含1到100范围内的默认值:

[ConfigurationProperty("waitForTimeSeconds", IsRequired=true, 
                       DefaultValue="10")]

这确实意味着该属性将有一个默认值,但我并不认为这是一个主要问题-我们是说它应该有一个在“合理”范围内的值,并且应该准备设置一个合理的默认值。

Microsoft链接今天已经用一个解决方案进行了更新。显然,如果未指定默认值,它将使用“0”作为默认值。当然,0超出了1-100的范围。“解决方案”是向ConfigurationProperty属性添加一个DefaultValue=参数,其默认值在范围内。不幸的是,这意味着您强加的默认值可能不是您想要/需要的。我也有这个问题。很高兴我偶然发现了这个问题!这就是我的工作。在我的例子中,我特别希望在配置文件中指定该选项,所以我不想设置默认值。但是,事实证明,如果您将一个字段标记为必需字段,那么事实将优先,并且默认值将永远不会实际使用,除非用于防止过早触发验证。这有点违反直觉,但它是有效的。
var Testing = ConfigurationManager.GetSection("TestingComponentSettings")
             as TestingComponentSettings;
[ConfigurationProperty("waitForTimeSeconds", IsRequired=true, 
                       DefaultValue="10")]