Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the ConfigurationManager.GetSection "system.webServer/handlers" not available?

I'm trying to read some configuration in my global.aspx Application_Start method. When I read ConfigurationManager.GetSection("system.web/httpHandlers") everything is fine:

ConfigurationManager.GetSection("system.web/httpHandlers") {System.Web.Configuration.HttpHandlersSection} base {System.Configuration.ConfigurationSection}: {System.Web.Configuration.HttpHandlersSection} Handlers: Count = 48

But when I read ConfigurationManager.GetSection("system.webServer/handlers") (which contains my custom handlers, it returns null. What am I doing wrong?

The section looks like this:

<system.webServer>
    <handlers>
        <add verb="*" path="*.loc" name="LocalizedResourceStreamer" 
                 type="CFW.WebUI.HttpHandlers.LocalizedResourceStreamer,WebUI" />
    </handlers>
</system.webServer>

Notes:

  • Web.configs are nested, ConfigurationManager.GetSection takes nesting into account by default.
  • The overall problem is trying to see if *.loc files are being served.

So far: enter image description here Looks like the system.webServer is ignored.

like image 586
Kees C. Bakker Avatar asked Jan 03 '12 13:01

Kees C. Bakker


2 Answers

Depending on your OS/setup, the system.webServer element may be configured to be ignored - and so the config system will not be constructing any inner configuration items from it. E.g. on my machine (WinXP, IIS 5.1), it's set to ignored by default.

Check the machine.config on the machine where this code is running, and see how the system.webServer element is configured. I don't have machines available with suitable later OSes at the moment, but it may be that this element is always set to be ignored - after all, that part of the config is for IIS' use, rather than our own.

like image 186
Damien_The_Unbeliever Avatar answered Sep 29 '22 21:09

Damien_The_Unbeliever


try :

**p.s. my web.config contains : <httpHandlers> and not handlers as yours. change as necessarily :) - also the webserver vs system.web **

enter image description here

   Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

        ConfigurationSection webConfigSections = webConfig.GetSection("system.web/httpHandlers");
        if (webConfigSections != null)
        {
         //   PropertyInformationCollection t = webConfigSections.ElementInformation.Properties;

            XDocument xmlFile = XDocument.Load(new StringReader(webConfigSections.SectionInformation.GetRawXml()));
            IEnumerable<XElement> query = from c in xmlFile.Descendants("add") select c;

            foreach (XElement band in query)
            {
            }


        }

p.s. thats the problem with this section - he doesnt have a uniquee element name that can be taken. thats why you take it whole("add" element) and parse it.

like image 45
Royi Namir Avatar answered Sep 29 '22 23:09

Royi Namir