我正在尝试读取 global.aspx
Application_Start
方法中的一些配置。当我读到ConfigurationManager.GetSection("system.web/httpHandlers")
一切都很好:
ConfigurationManager.GetSection(“system.web/httpHandlers”) {System.Web.Configuration.HttpHandlersSection} 基础{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 被忽略了。
根据您的操作系统/设置,
system.webServer
元素可能会配置为被忽略 - 因此配置系统不会从中构造任何内部配置项。例如。在我的机器(WinXP、IIS 5.1)上,它默认设置为忽略。
检查运行此代码的计算机上的
machine.config
,并查看 system.webServer
元素是如何配置的。我目前没有适用于后续操作系统的机器,但可能该元素始终被设置为忽略 - 毕竟,这部分配置是供 IIS 使用的,而不是我们自己的。
试试这个:
附注我的 web.config 包含:
<httpHandlers>
而不是你的 handlers
。根据需要进行更改:) - 还有 webserver
与 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)
{
}
}
附注这就是本节的问题 - 它没有可以采用的唯一元素名称。这就是为什么你要把它整个(“添加”元素)并解析它。