为什么ConfigurationManager.GetSection“system.webServer / handlers”不可用?

时间:2012-01-03 13:06:57

标签: c# asp.net iis web-config httphandler

我正在尝试在global.aspx Application_Start方法中阅读一些配置。当我阅读ConfigurationManager.GetSection("system.web/httpHandlers")时,一切都很好:

  

ConfigurationManager.GetSection( “的System.Web / HttpHandlers的”)   {} System.Web.Configuration.HttpHandlersSection       base {System.Configuration.ConfigurationSection}:{System.Web.Configuration.HttpHandlersSection}       处理程序:计数= 48

但是当我阅读ConfigurationManager.GetSection("system.webServer/handlers")(其中包含我的自定义处理程序时,它会返回null。我做错了什么?

该部分如下所示:

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

注意:

  • Web.configs是嵌套的,默认情况下ConfigurationManager.GetSection会考虑嵌套。
  • 整体问题是尝试查看是否正在提供* .loc文件。

到目前为止: enter image description here 看起来忽略 system.webServer

2 个答案:

答案 0 :(得分:3)

根据您的操作系统/设置,system.webServer元素可能被配置为被忽略 - 因此配置系统不会从中构造任何内部配置项。例如。在我的机器上(WinXP,IIS 5.1),它默认设置为忽略。

检查运行此代码的计算机上的machine.config,并查看system.webServer元素的配置方式。我目前没有合适的后期操作系统可用的机器,但可能是这个元素总是被设置为忽略 - 毕竟,配置的那部分是供IIS使用的,而不是我们自己的。

答案 1 :(得分:2)

尝试:

** P.S。我的web.config包含:<httpHandlers>而不包含handlers。必须改变:) - 也是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。这就是本节的问题 - 他没有可以采用的唯一元素名称。这就是为什么你把它全部(“添加”元素)并解析它。