自定义ASP.NET配置节

sta*_*ium 6 asp.net configuration web-config

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

<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>
...
Run Code Online (Sandbox Code Playgroud)

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

处理这个的类非常冗长,所以我不会在这里粘贴它,但可以从这里下载:

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

谢谢.

Kir*_*tan 1

您可以使用ConfigurationElementCollection 类

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

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

编辑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>
Run Code Online (Sandbox Code Playgroud)

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