【问题标题】:Iterate over elements in web.config遍历 web.config 中的元素
【发布时间】:2011-04-25 17:57:17
【问题描述】:

这次换个小问题...

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

<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>

干杯:)

【问题讨论】:

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


    【解决方案1】:

    using the ConfigurationLocation class.

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

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

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

    foreach (ConfigurationLocation location in config.Locations)
    {
    
    }
    

    【讨论】:

    • 我认为这绝对是这样做的方法,我实际上试图实现基于此的解决方案。问题是我收到“不在独立 exe 中运行时必须指定 exePath”。当我尝试打开配置时出错。
    • 我将更新我的答案并添加有关如何打开 web.config 的代码示例,但您可能还需要实现 IConfigurationSectionHandler。
    • 太棒了,我刚刚在另一篇文章中发现了这一点 :) 感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多