迭代web.config中的元素

时间:2011-04-25 17:57:18

标签: c# .net asp.net configuration iis-7

这次简短的问题需要改变......

有没有办法遍历web.config中的“location”元素?

<configuration>
    ...
    <location path="some/path">
        <system.web>
            <authorization users="*" />
        </system.web>    
    </location>
    <location path="some/other/path">
        <system.web>
            <authorization users="?" />
        </system.web>    
    </location>
    ...
<configuration>

...并且说输出类似于:

<table>
    <tr>
        <td>some/path</td>
        <td>authorization: *</td>
    </tr>
    <tr>
        <td>some/other/path</td>
        <td>authorization: ?</td>
    </tr>
</table>

干杯:)

1 个答案:

答案 0 :(得分:3)

using the ConfigurationLocation class

您可以像这样检索您的web.config:

Configuration config = WebConfigurationManager.OpenWebConfiguration("~/web.config");

从那里你可以遍历你的位置:

foreach (ConfigurationLocation location in config.Locations)
{

}