![](/img/trans.png)
[英]Automating IIS7 Website creation using impersonation (with Microsoft.Web.Administration)
[英]How do I manipulate Handler Mappings cleanly in IIS7 using the Microsoft.Web.Administration namespace?
使用Microsoft.Web.Administration
命名空间操作Handler Mappings时,有没有办法在站点级别删除<remove name="handler name">
。
例如,我有一个站点,它继承了全局处理程序映射配置中的所有处理程序映射。 在applicationHost.config
, <location>
标签最初看起来像这样:
<location path="60030 - testsite-60030.com">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication userName="" />
</authentication>
</security>
</system.webServer>
</location>
要删除处理程序,我使用类似的代码:
string siteName = "60030 - testsite-60030.com";
string handlerToRemove = "ASPClassic";
using(ServerManager sm = new ServerManager())
{
Configuration siteConfig =
serverManager.GetApplicationHostConfiguration();
ConfigurationSection handlersSection =
siteConfig.GetSection("system.webServer/handlers", siteName);
ConfigurationElementCollection handlersCollection =
handlersSection.GetCollection();
ConfigurationElement handlerElement = handlersCollection
.Where(h => h["name"].Equals(handlerMapping.Name)).Single();
handlersCollection.Remove(handlerElement);
}
这导致网站的<location>
标记看起来像:
<location path="60030 - testsite-60030.com">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication userName="" />
</authentication>
</security>
<handlers>
<remove name="ASPClassic" />
</handlers>
</system.webServer>
</location>
到现在为止还挺好。 但是,如果我重新添加ASPClassic
处理程序, ASPClassic
导致:
<location path="60030 - testsite-60030.com">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication userName="" />
</authentication>
</security>
<handlers>
<remove name="ASPClassic" />
<add name="ASPClassic" path="*.asp" verb="GET,HEAD,POST" modules="IsapiModule" scriptProcessor="%windir%\system32\inetsrv\asp.dll" resourceType="File" />
</handlers>
</system.webServer>
</location>
对于每个删除了处理程序然后以编程方式重新添加的网站,这可能会导致很多问题。 有没有办法只使用Microsoft.Web.Administration命名空间代码删除<remove name="ASPClassic" />
?
我已经与IIS产品团队讨论过这个问题,这似乎是配置系统的一个错误。 更有趣的是,当我使用IIS 7.5在Win7上尝试此代码时,我甚至无法以编程方式重新添加处理程序。 尝试这样做会导致COM异常,指出:
“错误:无法添加类型为'add'的重复集合条目,并将唯一键属性'name'设置为'ASPClassic'”
这变得更加成问题,因为一旦用户“删除”某个位置的处理程序,在修复此错误之前,无法通过MWA API重新添加该位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.