Search code examples
c#iis-7vbscriptiis-7.5appcmd

How to revert-to-parent handler mappings for an application on IIS 7 (or higher) by code?


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.


Solution

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