spwebonfigmodification以不期望的顺序添加子节点
-
10-12-2019 - |
题
我有一些代码使用spwebconfigmodification,将几个节点添加到system.webserver /模块部分。我要做的是添加一个生成的icoticetagcode,然后是一个生成的icetagcode。
无论我何种要做,web.config始终在<remove name="MyModule" />
之前始终使用<add name="MyModule" type="[type full assembly path]">
。这显然不正确,因为模块在添加后将删除。我要做的就是删除它,然后添加它。这是web.config最终看起来像:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="[type full assembly path]" />
<remove name="MyModule" />
</modules>
</system.webServer>
</configuration>
.
这是我想要它的样子:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="MyModule" />
<add name="MyModule" type="[type full assembly path]" />
</modules>
</system.webServer>
</configuration>
.
..而且这里是代码,它在spfeaturereceiver期间执行.featureInstalled:
SPSecurity.RunWithElevatedPrivileges(delegate
{
SPWebService spWebService = SPWebService.ContentService;
var moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer/modules",
Name = "remove[@name='MyModule']",
Sequence = 0,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<remove name='MyModule' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
spWebService.Update();
spWebService.ApplyWebConfigModifications();
moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer/modules",
Name = "add[@name='MyModule'][@type='[type full assembly path]']",
Sequence = 1000,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<add name='MyModule' type='type full assembly path' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
spWebService.Update();
spWebService.ApplyWebConfigModifications();
});
.
...我知道我应该能够在调用更新和应用程序Webconfigmodifications之前能够制作这些更改,但只需在此方法中拥有此代码,因为我无法弄清楚它为什么将添加的添加剂放在上面去掉。为什么这是这样做的?它试图按字母顺序排列吗?如何指定订单?
解决方案
Yes, the nodes are being alphabetically sorted and the Sequence
property only applies when the Name
property is exactly the same. To fix, use a little XPath trickery to get your nodes to sort in the correct order (first modules[1=1]
then modules[2=2]
):
SPSecurity.RunWithElevatedPrivileges(delegate
{
SPWebService spWebService = SPWebService.ContentService;
var moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer",
Name = "modules[1=1]/remove[@name='MyModule']",
Sequence = 0,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<remove name='MyModule' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer",
Name = "modules[2=2]/add[@name='MyModule'][@type='[type full assembly path]']",
Sequence = 1000,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<add name='MyModule' type='type full assembly path' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
spWebService.Update();
spWebService.ApplyWebConfigModifications();
});
Also, you might want to double check what happens when you call ApplyWebConfigModifications
to make sure your changes are propagated across the farm. Since you're calling it on an SPWebService I think it is OK but we always use the following:
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
Sources: