C# 迭代web.config中的元素
这次简短的问题需要改变 有没有一种方法可以遍历web.config中的“location”元素C# 迭代web.config中的元素,c#,.net,asp.net,configuration,iis-7,C#,.net,Asp.net,Configuration,Iis 7,这次简短的问题需要改变 有没有一种方法可以遍历web.config中的“location”元素 <configuration> ... <location path="some/path"> <system.web> <authorization users="*" /> </system.web> </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>
一些/路径
授权:*
一些/其他/路径
授权:?
干杯:)由
您可以如下方式检索web.config:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/web.config");
从那里,您可以遍历您的位置:
foreach (ConfigurationLocation location in config.Locations)
{
}
由
您可以如下方式检索web.config:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/web.config");
从那里,您可以遍历您的位置:
foreach (ConfigurationLocation location in config.Locations)
{
}
我认为这绝对是解决问题的方法,我实际上试图基于此实现一个解决方案。问题是,我得到了一个“当不在独立exe中运行时必须指定exePath”。当我尝试打开配置时出错。我将更新我的答案并添加一个关于如何打开web.config的代码示例,但您可能还需要实现IConfigurationSectionHandler.com,我刚刚在另一个帖子上发现:)谢谢你的回答!我认为这绝对是解决问题的方法,我实际上试图基于此实现一个解决方案。问题是,我得到了一个“当不在独立exe中运行时必须指定exePath”。当我尝试打开配置时出错。我将更新我的答案并添加一个关于如何打开web.config的代码示例,但您可能还需要实现IConfigurationSectionHandler.com,我刚刚在另一个帖子上发现:)谢谢你的回答!