Asp.net 如何在web.config中获取ConfigSection的列表

Asp.net 如何在web.config中获取ConfigSection的列表,asp.net,.net,Asp.net,.net,我的ASP.NET web服务在其web.config中定义了以下自定义配置部分: <configSections> <section name="config1" type="MyWebService.Configuration.MyWebServiceSection, App_Code"/> <section name="config2" type="MyWebService.Configuration.MyWebServiceSection,

我的ASP.NET web服务在其web.config中定义了以下自定义配置部分:

<configSections>
    <section name="config1" type="MyWebService.Configuration.MyWebServiceSection, App_Code"/>
    <section name="config2" type="MyWebService.Configuration.MyWebServiceSection, App_Code"/>
</configSections>
但我需要从web服务内部执行此操作,因此我没有指向可执行文件的路径。关于该答案的评论中建议使用
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
而不是可执行文件的路径,但这对我也不起作用(System.Argument异常:“当不在独立exe中运行时,必须指定exePath”)


这是正确的方法吗?如果是,当我在web服务中运行时,我应该传递什么到
OpenExeConfiguration
。您需要对web应用程序使用
WebConfigurationManager

var conf = WebConfigurationManager.OpenWebConfiguration("<path>")
foreach(var sec in conf.Sections)
{ 
  . . . . 
}
var conf=WebConfigurationManager.OpenWebConfiguration(“”)
foreach(配置部分中的变量秒)
{ 
. . . . 
}

这让我找到了正确的方向。但是我不得不改用OpenMappedWebConfiguration(),并首先将虚拟目录映射到物理路径……我不知道为什么必须这样做,但我似乎无法以其他方式使其工作。感谢您的帮助。这正是我的初衷——为您指出正确的配置管理器。是的,我忽略了一点,在ASP.NET中,您需要映射事物,因为您需要建立物理与虚拟的关系。这是我读到的。OpenMappedWebConfiguration:使用指定的文件映射将Web应用程序配置文件作为配置对象打开,以允许读或写操作。OpenWebConfiguration:使用指定的虚拟路径打开Web应用程序配置文件作为配置对象
,以允许读或写操作。
var conf = WebConfigurationManager.OpenWebConfiguration("<path>")
foreach(var sec in conf.Sections)
{ 
  . . . . 
}