Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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: Looks like the system.webServer is ignored.

    Something smells bad if you're having to read config sections like that (that "belong" to asp.net more than your app) – Damien_The_Unbeliever Jan 3, 2012 at 14:05 Well I need to read if *.loc files are handled. If they aren't handled I would like to throw an error on the application start, so it is not forgotten. – Kees C. Bakker Jan 3, 2012 at 14:17

    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.

    @KeesC.Bakker - I'd seriously suggest changing tack - perhaps whatever you're trying to achieve can be done without having to load that config section yourself? But we don't know what the overall problem is that you're trying to solve. – Damien_The_Unbeliever Jan 3, 2012 at 14:17 @KeesC.Bakker - the easiest way to be sure may be to simply make a request for one - otherwise, even if the handler is registered, you can't be 100% sure that IIS is routing requests for that extension to asp.net, so the handler would be unreachable. – Damien_The_Unbeliever Jan 3, 2012 at 15:41
       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.

    @ScottE each web.config ( by iis ver) is different. please change it to the one you need. - Ive also added a comment on that. thanks. – Royi Namir Jan 3, 2012 at 14:09 Also... a web.config might be nested, did you take that into account? My handler is actually registered in the "parent" application. – Kees C. Bakker Jan 3, 2012 at 14:11 @RoyiNamir I see what is the difference, I'm creating a class: namespace CFW.WebUI { public class Global : System.Web.HttpApplication Solution is part of a DLL. – Kees C. Bakker Jan 3, 2012 at 14:14 @KeesC.Bakker might be nested ?? you should write this ! Im not supposed to guess that. the solution i gave you Does help you read sections from web.config. please re-write your question with additions. – Royi Namir Jan 3, 2012 at 14:18

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.