We have created a virtual application programatically and updated the ApplicationHost.config file as below using ServerManager API available in Microsoft.Web.Administration.
<location path="Default Web Site/Test">
<system.webServer>
<directoryBrowse enabled="false" showFlags="Date, Time, Size, Extension" />
<handlers accessPolicy="Read, Execute, Script" />
<security>
<authentication>
<windowsAuthentication enabled="true">
<providers>
<clear />
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
<anonymousAuthentication enabled="true" />
<digestAuthentication enabled="false" />
<basicAuthentication enabled="false" />
</authentication>
</security>
<defaultDocument enabled="true">
<files>
<clear />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="default.aspx" />
</files>
</defaultDocument>
<modules runAllManagedModulesForAllRequests="true" runManagedModulesForWebDavRequests="true">
<remove name="RoleManager" />
</modules>
</system.webServer>
</location>
We have removed the application using the below code
string parent = "IIS://" + serverName + "/W3SVC/1/Root";
string directoryName = "Test";
using (DirectoryEntry iisRoot = new DirectoryEntry(parent))
{
// searching for the specified virtual Directory in root
foreach (DirectoryEntry dirEntry in iisRoot.Children)
{
if (dirEntry.Name.Equals(directoryName))
{
// deletes the virtual directory
parent.Children.Remove(entry);
break;
}
}
}
The problem is the applicationHost.config file is not cleaned completely still the below part remains
Note: I observed that when deleting the app in IIS management console removes all config settings from the applicationHost file but it doesn't clean on using DirectoryEntry code as above or using APPCMD (appcmd delete app "Default Web Site/Test")
<location path="Default Web Site/Test">
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" runManagedModulesForWebDavRequests="true">
<remove name="RoleManager" />
</modules>
</system.webServer>
</location>
This issue occurs only in IIS version 8.0 and above. How do we get a clean ApplicationHost.config file without any of the settings related to our "Test" virtual application when we delete the virtual application programatically?