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

Posted by Kev on Server Fault See other posts from Server Fault or by Kev
Published on 2009-11-05T03:56:56Z Indexed on 2010/03/21 14:41 UTC
Read the original article Hit count: 729

Filed under:
|
|

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>
        <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);
}

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>
        <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>
      <!-- Why doesn't <remove> get removed instead of tacking on 
           an <add> directive? -->
      <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 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?

© Server Fault or respective owner

Related posts about iis

Related posts about iis7