使用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>
对于每个删除处理程序然后以编程方式重新添加的网站,随着时间的推移,这可能会导致很多麻烦。有没有办法只删除<remove name="ASPClassic" />
using Microsoft.Web.Administration 命名空间代码?