When manipulating Handler Mappings using the Microsoft.Web.Administration
namespace, is there a way to remove the <remove name="handler name">
at the site level.
For example, I have a site which inherits all the handler mappings from the global handler mappings configuration. In applicationHost.config
the <location>
tag initially looks like this:
<location path="60030 - testsite-60030.com">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication userName="" />
</authentication>
</security>
</system.webServer>
</location>
To remove a handler I use code similar this:
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);
}
This results in the site's <location>
tag looking like:
<location path="60030 - testsite-60030.com">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication userName="" />
</authentication>
</security>
<handlers>
<remove name="ASPClassic" />
</handlers>
</system.webServer>
</location>
So far so good. However if I re-add the ASPClassic
handler this results in:
<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>
This can result in a lot of cruft over time for each website that's had a handler removed then re-added programmatically. Is there a way to just remove the <remove name="ASPClassic" />
using the Microsoft.Web.Administration namespace code?