gpt4 book ai didi

asp.net - 如何获取 web.config 中的 configSections 列表

转载 作者:行者123 更新时间:2023-12-02 10:23:25 25 4
gpt4 key购买 nike

我的 ASP.NET Web 服务在其 web.config 中定义了以下自定义 configSection:

<configSections>
<section name="config1" type="MyWebService.Configuration.MyWebServiceSection, App_Code"/>
<section name="config2" type="MyWebService.Configuration.MyWebServiceSection, App_Code"/>
</configSections>

在 WebMethod 中,我需要获取其中定义的节名称列表,在本例中为 "config1""config2"。来自 this answer ,我了解到独立应用程序可以通过使用以下内容来获取我正在寻找的内容:

Configuration config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);
foreach (ConfigurationSection cs in config.Sections)
{
sectionNames.Add(cs.SectionInformation.SectionName);
}

但我需要从 Web 服务内部执行此操作,因此我没有可执行文件的路径。在该答案的评论中建议使用 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 而不是可执行文件的路径,但这对我来说也不起作用(System.Argument 异常:“当不在独立的 exe 中运行时,必须指定 exePath”)。

这是正确的方法吗?如果是,当我在 Web 服务中运行时,我应该向 OpenExeConfiguration 传递什么?

最佳答案

对于 ASP.NET 使用这个。您需要将 WebConfigurationManager 用于 Web 应用程序

var conf = WebConfigurationManager.OpenWebConfiguration("<path>")
foreach(var sec in conf.Sections)
{
. . . .
}

https://msdn.microsoft.com/en-us/library/system.configuration.configuration.sections%28v=vs.110%29.aspx

关于asp.net - 如何获取 web.config 中的 configSections 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31948421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com