Application Defaults <applicationDefaults>

Overview

The <applicationDefaults> element of the <site> element specifies the default application settings for all applications in the parent site.

Note: If the same attribute or child element is configured in both the <applicationDefaults> section and in the <application> section for a specific application, the configuration in the <application> section is used for that application.

Compatibility

Version Notes
IIS 7.5 The <application> element was updated to include the serviceAutoStartEnabled and serviceAutoStartProvider attributes.
IIS 7.0 The <application> element of the <site> collection was introduced in IIS 7.0.
IIS 6.0 The <applicationDefaults> element is analogous to setting application options at the site level in the IIS 6.0 metabase.

Setup

The <applicationDefaults> element of the <site> element is included in the default installation of IIS 7.

How To

How to configure the default application settings for a site

  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, expand the server name, expand the Sites node, then click the name of the site.
  3. In the site's Home pane, click View Applications in the Actions pane.
  4. In the site's Application pane, click Set Application Defaults... in the Actions pane.
  5. In the Application Defaults dialog box, specify your default application settings for the site, and then click OK.

Configuration

Attributes

Attribute Description
applicationPool Optional string attribute.

Specifies the default application pool to which all applications in the parent site are assigned.
enabledProtocols Optional string attribute.

Specifies the protocols to use to communicate with all applications in the parent site.
path Optional string attribute.

Specifies the default virtual path of all applications in the parent site.
serviceAutoStartEnabled Optional Boolean attribute.

true if the autostart is enabled for this application; otherwise, false.

The default value is false.
serviceAutoStartProvider Optional string attribute.

Specifies the name of the autostart provider that the Windows Process Activation Service (WAS) will use if serviceAutoStartEnabled is set to true.

There is no default value.

Child Elements

None.

Configuration Sample

The following configuration sample sets the default application pool for the Default Web Site to "DefaultAppPool."

<sites>
   <site name="Default Web Site" id="1" serverAutoStart="true">
      <application path="/" applicationPool="Classic .NET AppPool">
         <virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
      </application>
      <bindings>
         <binding protocol="http" bindingInformation="*:80:" />
      </bindings>
      <applicationDefaults applicationPool="DefaultAppPool" />
   </site>
</sites>

Sample Code

The following code samples set the default application pool for the Default Web Site to "DefaultAppPool."

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].applicationDefaults.applicationPool:DefaultAppPool" /commit:apphost

Note: You must be sure to set the commit parameter to apphost when you use AppCmd.exe to configure these settings. This commits the configuration settings to the appropriate location section in the ApplicationHost.config file.

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");

         ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
         ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Default Web Site");
         if (siteElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElement applicationDefaultsElement = siteElement.GetChildElement("applicationDefaults");
         applicationDefaultsElement["applicationPool"] = @"DefaultAppPool";

         serverManager.CommitChanges();
      }
   }

   private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
   {
      foreach (ConfigurationElement element in collection)
      {
         if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
         {
            bool matches = true;
            for (int i = 0; i < keyValues.Length; i += 2)
            {
               object o = element.GetAttributeValue(keyValues[i]);
               string value = null;
               if (o != null)
               {
                  value = o.ToString();
               }
               if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
               {
                  matches = false;
                  break;
               }
            }
            if (matches)
            {
               return element;
            }
         }
      }
      return null;
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")

      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Default Web Site")
      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim applicationDefaultsElement As ConfigurationElement = siteElement.GetChildElement("applicationDefaults")
      applicationDefaultsElement("applicationPool") = "DefaultAppPool"

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");

var sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "Default Web Site"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);

var applicationDefaultsElement = siteElement.ChildElements.Item("applicationDefaults");
applicationDefaultsElement.Properties.Item("applicationPool").Value = "DefaultAppPool";

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")

Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Default Web Site"))
If (siteElementPos = -1) Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)

Set applicationDefaultsElement = siteElement.ChildElements.Item("applicationDefaults")
applicationDefaultsElement.Properties.Item("applicationPool").Value = "DefaultAppPool"

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function
Deprecated Elements