IIS7 Handler Mapping Migration from Sites Config to Server Config

iis-7

We have a bunch of sites running with about 8 handler mappings in their web.config files. Unfortunately, they were getting copied site to site every time a new one was added.

Now the time has come for me to get these out of all the web.config's and get them into the server's Handler Mappings.

If I add the mapping to the the server while it still exists in the web.config, IIS throws an error when you browse to the site. I have a few dozen web.config's to edit here with about 10 mappings in each.

Is there a way to add these mappings to the server without having to go in an edit each web.config file manually? Otherwise, every site will be down for a few minutes while I go into each file and remove the handlers.

Thanks!

Best Answer

You really have no choice other here if you're doing this manually. Even if you did just one handler mapping at a time via the UI you're either going to have to have both the site and the global handler installed at the same time (albeit briefly) or remove the local site one then re-add to the global settings.

You could of course script this using appcmd.exe:

http://www.iis.net/ConfigReference/system.webServer/handlers

For example:

To remove a handler from a site (I'm using the Default Web Site as an example) -

appcmd set config "Default Web Site" -section:system.webServer/handlers /-"[name='MyHandler']"

To add a handler to the global or "APP HOST" config:

appcmd set config -section:system.webServer/handlers /+"[name='MyHandler',
    path='*.mything',modules='MyApp.MyHandler',preCondition='integratedMode',
    resourceType='Unspecified',verb='GET,HEAD,POST']" 
    /commit:apphost

I've split the above command over four lines just to make it readable, it should in fact be just one line.

Using a script would mean your interruption time would be around a minute rather than several.