【问题标题】:Custom ASP.NET Configuration Section自定义 ASP.NET 配置部分
【发布时间】:2010-11-12 13:59:41
【问题描述】:

我想创建一个自定义配置部分来处理电子邮件通知。配置需要采用以下格式:

<configSections>
    <sectionGroup name="notifications">
        <section name="notification" type="NotificationConfiguration" allowLocation="true" allowDefinition="Everywhere" />
    </sectionGroup>
</configSections>
...
<notifications>
    <notification name="..." enabled="..." delayInMinutes="...">
        <recipients>
            <add email="..." />
            <add email="..." />
            <add email="..." />
        </recipients>
    </notification>
    <notification name="..." enabled="..." delayInMinutes="...">
        <recipients>
            <add email="..." />
            <add email="..." />
            <add email="..." />
        </recipients>
    </notification>
</notifications>
...

我可以使用NotificationConfiguration config = (NotificationConfiguration) ConfigurationManager.GetSection("notifications\notification") 使其正常工作,但这仅适用于一个&lt;notification&gt; 元素。如何完成多个元素以容纳多个通知?

处理这个的类比较长,这里就不贴了,但是可以从这里下载:

http://files.getdropbox.com/u/288235/NotificationConfiguration.cs

谢谢。

【问题讨论】:

  • 您可以创建一个 外部元素,然后将所有通知元素放入该组中。这样,您将能够实现您想要实现的目标。
  • 检查我更新的答案。

标签: asp.net configuration web-config


【解决方案1】:

您可以使用ConfigurationElementCollection Class

如何使用它的参考可以在CodeProject找到。

编辑:您可以创建一个&lt;NotificationsGroup /&gt; 外部元素,然后将所有通知元素放入该组中。这样,您将能够实现您想要实现的目标。

编辑 2:

<configSections>
    <sectionGroup name="NotificationsGroup">
        <section name="NotificationsGroup" type="NotificationGroupConfiguration" allowLocation="true" allowDefinition="Everywhere" />
    </sectionGroup>
</configSections>

<NotificationsGroup>
    <Notifications>
    </Notifications>
    ... Multiple notifications go here, instead of one.
    <Notifications>
    </Notifications>
</NotificationsGroup>

这意味着NotificationsGroup 将包含通知的元素集合。

【讨论】:

  • 嗨 Kirtan 我的理解是 ConfigurationElementCollection 类用于元素的集合,而不是部分。我将该类用于 标记,但 标记被定义为 web.config 文件顶部的配置部分,所以我如何完成部分的集合,而不是元素的集合在一个部分内?
  • 在 web.config 的顶部,我将自定义配置定义如下:
    你是指一组通知,就像我的 标签中的那样?
  • 感谢您的帮助 Kirtran。下面的工具最终也有很大帮助! codeplex.com/csd
猜你喜欢
  • 2010-10-25
  • 2010-10-04
  • 2013-09-17
  • 2011-07-04
  • 2012-12-12
  • 2013-09-21
  • 2012-05-11
  • 2014-01-19
相关资源
最近更新 更多