我正在尝试在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>
注意:
ConfigurationManager.GetSection
会考虑嵌套。到目前为止:
看起来忽略 system.webServer 。
答案 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 **
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。这就是本节的问题 - 他没有可以采用的唯一元素名称。这就是为什么你把它全部(“添加”元素)并解析它。