The <application> element controls the configuration settings for a specific application. An application in Internet Information Services (IIS) 7.0 is a grouping of files that delivers content or provides services over protocols, such as HTTP. When you create an application in IIS 7.0, the application's path becomes part of the site's URL.
In IIS 7.0, each site must have at least one application, called the root or default application. However, a site can have more than one application. For example, your online commerce Web site can contain several applications, such as a shopping cart application that lets users gather items during shopping and a login application that allows users to recall saved payment information when they make a purchase.
An application must contain at least one virtual directory, called the root or default virtual directory with configuration settings defined in a <virtualDirectory> element.
| |
IIS 7.0 |
IIS 6.0 |
| Notes |
The <application> element is new in IIS 7.0. |
The <application> element replaces the IIS 6.0 IIsWebVirtualDir and IIsWebDirectory metabase objects.
|
The <application> element is included in the default installation of IIS 7.0.
How to create a new Web application
- Click Start, click Administrative Tools, and then click Internet Information Services (IIS) Manager.
- In the Connections pane, expand the server name, expand Sites, and then click the Web site to which you want to add an application.
- In the Actions pane, click View Applications, and then click Add Application...
- In the Add Application dialog box, at a minimum enter information in the Alias: and Physical path: text boxes, and then click OK.
Note: You can click Select... to choose a different application pool, click Connect as... to configure the application to accept credentials from a specific account, and click Test Settings... to test authentication and authorization settings for the application.
The <application> element is configurable at the server level in the ApplicationHost.config file.
Attributes
| Attribute |
Description |
applicationPool |
Optional string attribute.
Specifies the application pool to which the application is assigned. |
enabledProtocols |
Optional string attribute.
Specifies the protocols through which to communicate with the application. This value enables HTTP and HTTPS protocols for the application.
The default value is http. |
path |
Required string attribute.
Specifies the virtual path of the application. Must be a unique path within the parent site. |
Child Elements
| Element |
Description |
virtualDirectory |
Optional element.
Specifies configuration settings for a virtual directory in the parent application. |
virtualDirectoryDefaults |
Optional element.
Specifies the default settings for all virtual directories in the parent application. |
Configuration Sample
The following configuration example displays a <site> element that contains two applications. The first <application> element defines the root application for the site, along with the root virtual directory for the application. The second <application> element contains the configuration settings for a CRM application on the Web site. The path attribute defines the path for the application. This <application> element contains two <virtualDirectory> elements. The first defines the root virtual directory for the application, and the second defines an Images virtual directory for the application.
<site name="Contoso" id="2" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="C:\Contoso\Content" />
</application>
<application path="/CRM">
<virtualDirectory path="/" physicalPath="C:\Contoso\Content\CRM" />
<virtualDirectory path="/Images" physicalPath="E:\Images" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:www.contoso.com" />
</bindings>
</site>
The following examples create a new application named ShoppingCart with a virtual directory root of C:\Inetpub\Contoso\ShoppingCart for a site that is named Contoso.
Note: When you create an application from the command prompt or from a script, you must explicitly create a root virtual directory for the application.
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].[path='/ShoppingCart']" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].[path='/ShoppingCart'].[path='/',physicalPath='C:\Inetpub\Contoso\ShoppingCart']" /commit:apphost
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", @"Contoso");
if (siteElement == null) throw new InvalidOperationException("Element not found!");
ConfigurationElementCollection siteCollection = siteElement.GetCollection();
ConfigurationElement applicationElement = siteCollection.CreateElement("application");
applicationElement["path"] = @"/ShoppingCart";
ConfigurationElementCollection applicationCollection = applicationElement.GetCollection();
ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory");
virtualDirectoryElement["path"] = @"/";
virtualDirectoryElement["physicalPath"] = @"C:\Inetpub\Contoso\ShoppingCart";
applicationCollection.Add(virtualDirectoryElement);
siteCollection.Add(applicationElement);
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", "Contoso")
If (siteElement Is Nothing) Then
Throw New InvalidOperationException("Element not found!")
End If
Dim siteCollection As ConfigurationElementCollection = siteElement.GetCollection
Dim applicationElement As ConfigurationElement = siteCollection.CreateElement("application")
applicationElement("path") = "/ShoppingCart"
Dim applicationCollection As ConfigurationElementCollection = applicationElement.GetCollection
Dim virtualDirectoryElement As ConfigurationElement = applicationCollection.CreateElement("virtualDirectory")
virtualDirectoryElement("path") = "/"
virtualDirectoryElement("physicalPath") = "C:\Inetpub\Contoso\ShoppingCart"
applicationCollection.Add(virtualDirectoryElement)
siteCollection.Add(applicationElement)
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", "Contoso"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);
var siteCollection = siteElement.Collection;
var applicationElement = siteCollection.CreateNewElement("application");
applicationElement.Properties.Item("path").Value = "/ShoppingCart";
var applicationCollection = applicationElement.Collection;
var virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory");
virtualDirectoryElement.Properties.Item("path").Value = "/";
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\\Inetpub\\Contoso\\ShoppingCart";
applicationCollection.AddElement(virtualDirectoryElement);
siteCollection.AddElement(applicationElement);
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 = 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", "Contoso"))
If (siteElementPos = -1) Then
WScript.Echo "Element not found!"
WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)
Set siteCollection = siteElement.Collection
Set applicationElement = siteCollection.CreateNewElement("application")
applicationElement.Properties.Item("path").Value = "/ShoppingCart"
Set applicationCollection = applicationElement.Collection
Set virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory")
virtualDirectoryElement.Properties.Item("path").Value = "/"
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\Inetpub\Contoso\ShoppingCart"
applicationCollection.AddElement(virtualDirectoryElement)
siteCollection.AddElement(applicationElement)
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 value = Null Then
value = CStr(value)
End If
If Not value = 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