Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of configSections in web.config

Tags:

.net

asp.net

My ASP.NET web service has the following custom configSections defined in its web.config:

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

From within a WebMethod, I need to get the list of section names defined there, which in this example would be "config1" and "config2". From this answer, I learned that a standalone application can get what I'm looking for by using something like:

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

but I need to do this from inside a web service, so I don't have a path to an executable. There is a suggestion in the comments on that answer to use ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); instead of the path to the executable, but that didn't work for me either (System.Argument exception: "exePath must be specified when not running inside a stand alone exe").

Is this the right approach, and if so, what should I pass to OpenExeConfiguration when I'm running inside a web service?

like image 250
Jeff Loughlin Avatar asked Dec 09 '25 07:12

Jeff Loughlin


1 Answers

For asp.net use this. You need to use WebConfigurationManager for web applications

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

like image 84
T.S. Avatar answered Dec 11 '25 00:12

T.S.