.Net是从上到下读取web.config的吗?
1) 如果(在 web.config 文件内)我在
之前声明名为 songPoem 的自定义部分,则会报告错误 songPoem 元素无法识别。 因此,以下给出了一个错误:
<songPoem song=”lalala” />
<configSection>
<section name=”songPoem” type=”A” />
</configSection>
虽然以下工作正常:
<configSection>
<section name=”songPoem” type=”A” />
</configSection>
<songPoem song=”lalala” />
A)我假设错误是由于.Net从上到下读取web.config
?! 如果是这样,那么元素声明的顺序是否仅在涉及自定义部分元素时才成为问题,还是......?
顺便说一句 - 这是 A 类的定义:
public class A: ConfigurationSection
{
[ConfigurationProperty(“song”)]
public string Song{ ... }
}
2)我假设
元素中只允许使用 song 属性,因此我希望 .Net将能够注意到自定义部分元素是否包含任何不存在的属性。 但由于某种原因,我也能够包含其他属性,即使它们没有映射到类 A 的任何属性:
<songPoem song=”lalala” movie=”this_should_be_here” />
知道为什么 Net 没有注意到 songPoem > 包含无效属性?
1) If ( inside web.config file ) I declare custom section named songPoem before <configSection>
, an error is reported saying songPoem element is not recognized. Thus, the following gives me an error:
<songPoem song=”lalala” />
<configSection>
<section name=”songPoem” type=”A” />
</configSection>
while the following works just fine:
<configSection>
<section name=”songPoem” type=”A” />
</configSection>
<songPoem song=”lalala” />
A) I assume error is due to .Net reading web.config
from top to bottom?! If so, is the order of element declaration an issue only when it comes to custom section elements, or...?
BTW - here's the definition for class A:
public class A: ConfigurationSection
{
[ConfigurationProperty(“song”)]
public string Song{ ... }
}
2) I would assume that only song attribute would be allowed inside <songPoem>
element, and thus I would expect that .Net would be able to notice if custom section element includes any non existing attributes. But for some reason I was able to include other attributes also, even though they don’t map to any property of class A:
<songPoem song=”lalala” movie=”this_should_be_here” />
Any idea why Net didn't notice that songPoem contains an invalid attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不久前写了一篇关于自定义配置部分的相当广泛的博客文章,以澄清我自己对它们的困惑。 这应该会给您一些提示来帮助您解决问题:
http: //www.endswithsaurus.com/2008/11/custom-configuration-section.html
I did quite an extensive blog post on custom configuration sections a while back to clear up my own confusion about them. That should give you some pointers to help you get it sorted out:
http://www.endswithsaurus.com/2008/11/custom-configuration-section.html
我总是像这样设置我的 web.config:
从来没有任何问题。 当我犯了和你一样的错误并且无法理解问题所在时,这真的让我抓狂。
I've always setup my web.config like so:
Never had any problems. It truely made me nuts when I made the same mistake as you and couldn't understand what the issue was.
好吧,它使用 xmlreader 来读取数据,所以我想它会从上到下读取,并按顺序处理它找到的元素。
尽管我怀疑它会进行双重传递来支持此类问题。 我想不会的。
另外,我总是看到在顶部声明配置部分的标准做法,也许这就是原因。
从技术上讲,顺序也很重要,因为就像反序列化一样,我希望列表中的对象按照它们定义的顺序进行反序列化。
well, it uses an xmlreader to read the data, so I would imagine it reads top down, and processes the elements it finds in order.
Though i would suspect it to do a double-pass to support issues like this. I guess it doesn't.
Also, i've always seen a standard practice of declaring config sections at the top, perhaps this is why.
Technically order matters too, because just as with deserialization, i would expect the objects in a list to be deserialized in the same order they were defined.