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

8,671

Solution 1

I also asked this over on Stack Overflow because the subject matter straddled both sites. This is the answer I got:

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

Solution 2

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.

Share:
8,671

Related videos on Youtube

Kev
Author by

Kev

###Actively looking for freelance work ###About Me: I'm a professional software developer and have spent my time building provisioning and web based self-service systems for IIS, Apache and Citrix XenServer, amongst other things. My Curriculum Vitae can be viewed on Stack Overflow Careers (might be a bit out of date). Stuff I like to listen to at last.fm You can get in touch here: kevin.e.kenny #@# gmail.com (you know what to do with the # and spaces). No Survey Emails Please. Also not ashamed to admit I like trains, mostly diesels, late Era 8 (BR Sectorisation) and Era 9 onwards :) I'm also interested in signalling if anyone from Network Rail is looking this far down ;)

Updated on September 17, 2022

Comments

  • Kev
    Kev over 1 year

    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?

  • Kev
    Kev about 14 years
    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/…
  • MikeT
    MikeT about 14 years
    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.