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

StackOverflow https://stackoverflow.com/questions/1671702

  •  13-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

I have discussed this with the IIS product team and this appears to be a bug with the configuration system. What is more interesting is that when I attempt this code on Win7 with IIS 7.5, I cannot even re-add the handler programmatically. Attempting to do so results in a COM exception that states:

"Error: Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'ASPClassic'"

That becomes even more problematic because once a user has "removed" a handler for a location, it cannot be re-added through the M.W.A. API until this bug is fixed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top