[英]How to read custom section from web.config?
I am implementing some IIS module to work with web.config of application. 我正在实现一些IIS模块以与应用程序的web.config一起使用。 I already had working part of module, but I managed to read custom section from web.config.
我已经有模块的工作部分,但是我设法从web.config中读取自定义部分。
I added the following IIS schema for my custom section to standard folder %windir%\\system32\\inetsrv\\config\\schema\\ : 我将自定义部分的以下IIS模式添加到标准文件夹%windir%\\ system32 \\ inetsrv \\ config \\ schema \\中:
<configSchema>
<sectionSchema name="myCustomSection">
<element name="customCollection">
<collection addElement="customItem">
<attribute name="name" type="string" />
<attribute name="path" type="string" />
</collection>
</element>
</sectionSchema>
</configSchema>
The following XML fits to the sectionSchema: 以下XML适合sectionSchema:
<configuration>
<myCustomSection>
<customCollection>
<customItem name="item1" path="D:\item1\web.config" />
<customItem name="item2" path="D:\item2\web.config" />
<customItem name="item3" path="D:\item3\web.config" />
</customCollection>
</myCustomSection>
...
</configuration>
My code can read section myCustomSection and element customCollection, but customCollection is empty =( 我的代码可以读取myCustomSection部分和元素customCollection,但customCollection为空=(
Code to read: 读取的代码:
[ModuleServiceMethod(PassThrough = true)]
public PropertyBag GetAppSettings(string applicationPath)
{
var bag = new PropertyBag();
var manager = this.ManagementUnit.ServerManager;
var configurationPath = ManagementConfigurationPath.CreateApplicationConfigurationPath(ManagementUnit.ConfigurationPath.SiteName, applicationPath);
var effectiveConfigurationPath = configurationPath.GetEffectiveConfigurationPath(ManagementScope.Server);
var configuration = manager.GetWebConfiguration(ManagementUnit.ConfigurationMap, effectiveConfigurationPath);
var claimsSection = configuration.GetSection("myCustomSection");
var child = claimsSection.GetChildElement("customCollection");
var childCollection = child.GetCollection(); //count = 0
// var childCollection = child.GetCollection("customItem"); throw exception
...
return bag;
}
May someone know how to get children from collection? 可能有人知道如何让孩子们从藏品中领取吗?
UPDATE 更新
Unfortunately, I passed wrong applicationPath to this function. 不幸的是,我将错误的applicationPath传递给此函数。 Moreover, there is neater code to do the same stuff:
此外,还有更整洁的代码可以执行相同的操作:
var section = ManagementUnit.Configuration.GetSection("myCustomSection");
var child = sect.GetChildElement("customCollection");
var childCollection = child.GetCollection();
Get raw XML From section and process that XML to get attribute values. 从节中获取原始XML,并处理该XML以获取属性值。
var claimsSection = config.GetSection("myCustomSection");
string xml = claimsSection.SectionInformation.GetRawXml();
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.LoadXml(xml);
System.Xml.XmlNode xList = xDoc.ChildNodes[0];
for (int i =0; i< xList.ChildNodes.Count; i++)
{
foreach (System.Xml.XmlNode xNodo in xList.ChildNodes[i])
{
Console.WriteLine((xNodo.Attributes[0].Value));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.