Question

It is possible to revert handler mappings for an application to parent manually by following below steps:

  • Select Application on IIS management console
  • Double click Handler Mappings
  • Clik Revert to Parent on the Actions pane

Is there a way achieve the same task programmatically (appcmd.exe, VBScript, C#..) ?

Note: If there is a custom setting already defined for the application, aspnet_regiis -i command does not work. The only way I have found so far is to remove application from IIS and add it again by code.

Was it helpful?

Solution 2

The following works for the question:

using System;
using System.Text;
using System.DirectoryServices;

namespace RevertToParentHandlerMappings
{
    class Program
    {
        static void Main(string[] args)
        {

            string vDirPath = "IIS://localhost/W3SVC/1/ROOT/AppName";

            DirectoryEntry vDir = new DirectoryEntry(vDirPath);

            vDir.Properties["ScriptMaps"].Clear();

            vDir.CommitChanges();

        }
    }
}

OTHER TIPS

I'm having the unfortunate task of migrating several IIS6 servers to IIS8.5. My dry run last week worked pretty well just using web deploy to copy everything over. I had only a few nagging issues that were easily fixed by fixing one or two of the websites. I restored my IIS backup to before I ran web deploy last week, then reran web deploy to copy everything back over like I did last week(due to several new sites being created since last week). To my shock, I ran into all the horrible error messages that are fixed when you revert to parent on the actions page for handler mappings.

Since there's 570 some odd sites and for some crazy reason, it takes 18 seconds to go through the process of selecting a site, selecting handler mappings, clicking revert, answering the question, clicking the next site and it loading. It would take me nearly 3 hours to take care of this server if I didn't pause for even a second.

I'm eternally grateful to have found this topic. I wrote a quick console app to run through and revert to parent on all my sites. I'll take a little over 2 minutes over 2.8 hours any day! I just thought I'd share the full thing in case someone else runs into this issue.

using System;
using System.DirectoryServices;
using System.Collections;

namespace IISHandlerMappingsRevertToParentAll
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args == null || args.Length != 3)
            {
                Console.WriteLine("IISHandlerMappingsRevertToParentAll.exe WebServer UserName Password");
                Console.WriteLine("Example: IISHandlerMappingsRevertToParentAll WebServer1 WebServer1\\MyUserName MyPassword");
            }
            else
            {
                DateTime dt = DateTime.Now;
                int cleared = 0;
                using (DirectoryEntry root = new DirectoryEntry("IIS://" + args[0] + "/W3SVC", args[1], args[2], AuthenticationTypes.FastBind))
                {
                    foreach (DirectoryEntry r in root.Children)
                    {
                        if (r.SchemaClassName == "IIsWebServer")
                        {
                            foreach (DirectoryEntry e in r.Children)
                            {
                                if (e.SchemaClassName == "IIsWebVirtualDir")
                                {
                                    ArrayList ScriptMaps = new ArrayList(e.Properties["ScriptMaps"]);
                                    if (ScriptMaps.Count > 0)
                                    {
                                        Console.WriteLine("Clearing for " + e.Properties["Path"].Value);
                                        cleared++;
                                        e.Properties["ScriptMaps"].Clear();
                                        root.CommitChanges();
                                    }
                                }
                            }
                        }
                    }
                }
                Console.WriteLine("Reset " + cleared + " took " + DateTime.Now.Subtract(dt).TotalSeconds + "s ");
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top