How do I manipulate Handler Mappings cleanly in IIS7 using the Microsoft.Web.Administration namespace?

I asked this over on Stack Overflow but maybe it's something an experienced IIS 7 administrator might know more about, so I'm asking here as well.

When manipulating Handler Mappings using the Microsoft.Web.Administration namespace, is there a way to remove the <remove name="handler name"> tag added 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>
</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();
siteConfig.GetSection("system.webServer/handlers", siteName);
ConfigurationElementCollection handlersCollection =

ConfigurationElement handlerElement = handlersCollection
.Where(h => h["name"].Equals(handlerMapping.Name)).Single();

handlersCollection.Remove(handlerElement);
}


The equivalent APPCMD instruction would be:

appcmd set config "60030 - autotest-60030.com" -section:system.webServer/handlers /-[name='ASPClassic'] /commit:apphost


This results in the site's <location> tag looking like:

<location path="60030 - testsite-60030.com">
<system.webServer>
<security>
<authentication>
</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>
</authentication>
</security>
<handlers>
<!-- Why doesn't <remove> get removed instead of tacking on
<remove name="ASPClassic" />
</handlers>
</system.webServer>
</location>


This happens when using both the Microsoft.Web.Administration namespace and C# or using the following APPCMD command:

appcmd set config "60030 - autotest-60030.com" -section:system.webServer/handlers /+[name='ASPClassic',path='*.asp',verb=;'GET,HEAD,POST',modules='IsapiModule',scriptProcessor='%windir%\system32\inetsrv\asp.dll',resourceType='File'] /commit:apphost


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" /> tag using the Microsoft.Web.Administration namespace code or APPCMD?

-

If you add or remove a handler mapping/module manually on a particular site/application/virdir you will get the <remove /> tag added. Now, if you manually add it again the <remove /> tag won't go because as for IIS that means you made some changes in the configuration. This way you it gives you the flexibility to add your own custom mapping or module overriding the global modules/mappings.

Yes, that will produce a lot of clutter but that's the way it's been handled.. at least for now. One way to handle this is click on 'Revert to parent' in the Actions panel.

-
Sorry dude, I asked this over on SO as well because it was a bit SO and a bit SF. I got this reply from a MS employee (Tobin Titus), it's a bug: stackoverflow.com/questions/1671702/… –  Kev Mar 22 '10 at 11:40
Yes, I know this is a bug. I am an MSFT as well working in IIS & ASP.NET team, and we have come across this issue some time back and the product team is working on this. –  Vivek Kumbhar Mar 22 '10 at 12:43