【发布时间】:2009-11-04 04:46:32
【问题描述】:
当使用 Microsoft.Web.Administration 命名空间操作处理程序映射时,有没有办法在站点级别删除 <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 处理程序,则会导致:
<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" />?
【问题讨论】: