Sites <sites>
Overview
The <sites>
section group contains configuration settings for all sites on an IIS 7 server. The <sites>
element contains a collection of <site>
elements. When you create a new Web site, IIS adds a <site>
element to the collection.
Each <site>
element contains configuration settings for a separate Web site hosted on your IIS 7 installation. For example, the Default Web Site and its settings are defined in a <site>
element that is located between the opening and closing tags of the <sites>
element; configuration elements for any additional site that you create are also located there.
The <sites>
section group can also contain the <siteDefaults>
, <applicationDefaults>
, and <virtualDirectoryDefaults>
elements. The <siteDefaults>
element defines default configuration settings for any site running on the server, the <applicationDefaults>
element defines default configuration settings for any application running on the server, and the <virtualDirectoryDefaults>
element defines default configuration settings for any virtual directory running on the server.
Compatibility
Version | Notes |
---|---|
IIS 10.0 | The <sites> element was not modified in IIS 10.0. |
IIS 8.5 | The <sites> element was not modified in IIS 8.5. |
IIS 8.0 | The <sites> element was not modified in IIS 8.0. |
IIS 7.5 | The <sites> element was not modified in IIS 7.5. |
IIS 7.0 | The <sites> element was introduced in IIS 7.0. |
IIS 6.0 | The <sites> collection replaces the IIS 6.0 IIsWebServer metabase object. |
Setup
The <sites>
element is included in the default installation of IIS 7.
How To
When you configure a new Web site on IIS 7, you must assign the Web site a site name and a physical path. There are also a number of optional configuration settings you can set. If you plan to continue to use the Default Web Site on your IIS 7 server, you must alter the binding information for the new site. You can do this by changing either the port or entering a host name for the new Web site.
How to create a new Web site
Open Internet Information Services (IIS) Manager:
If you are using Windows Server 2012 or Windows Server 2012 R2:
- On the taskbar, click Server Manager, click Tools, and then click Internet Information Services (IIS) Manager.
If you are using Windows 8 or Windows 8.1:
- Hold down the Windows key, press the letter X, and then click Control Panel.
- Click Administrative Tools, and then double-click 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.
In the Connections pane, expand the server name, and then click Sites.
In the Actions pane, click Add Web Site...
In the Add Web Site dialog box, at a minimum, enter information in the Site name and Physical path text boxes and choose whether you want to enter information in the Host name text box or change the number in the Port box.
On IIS Manager click the refresh button to verify that the site has started.
Configuration
You configure the <sites>
element at the server level in the ApplicationHost.config file.
Attributes
None.
Child Elements
Element | Description |
---|---|
applicationDefaults |
Optional element. Specifies default settings for all applications on the server. |
site |
Optional element. Specifies configuration settings for a site. |
siteDefaults |
Optional element. Specifies default settings for all sites on the server. |
virtualDirectoryDefaults |
Optional element. Specifies default settings for all virtual directories on the server. |
Configuration Sample
The following default <sites>
element is configured in the root ApplicationHost.config file in IIS 7.
<sites>
<site name="Default Web Site" id="1">
<application path="/">
<virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:" />
</bindings>
</site>
<siteDefaults>
<logFile logFormat="W3C" directory="%SystemDrive%\inetpub\logs\LogFiles" />
<traceFailedRequestsLogging directory="%SystemDrive%\inetpub\logs\FailedReqLogFiles" />
</siteDefaults>
<applicationDefaults applicationPool="DefaultAppPool" />
<virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>
Compatibility
In combination with the <site>
element, the <sites>
element replaces the IIS 6.0 IIsWebServer metabase property.
Sample Code
The following examples create a new Web site named Contoso with an ID of 2, and sets a binding for the HTTP protocol over port 80 with a host header of "www.contoso.com". The physical path for the new Web site is C:\Inetpub\www.contoso.com\wwwroot.
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2',serverAutoStart='True']" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2'].bindings.[protocol='http',bindingInformation='*:80:www.contoso.com']" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2'].[path='/']" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso',id='2'].[path='/'].[path='/',physicalPath='C:\Inetpub\www.contoso.com\wwwroot']" /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 = sitesCollection.CreateElement("site");
siteElement["name"] = @"Contoso";
siteElement["id"] = 2;
siteElement["serverAutoStart"] = true;
ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings");
ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
bindingElement["protocol"] = @"http";
bindingElement["bindingInformation"] = @"*:80:www.contoso.com";
bindingsCollection.Add(bindingElement);
ConfigurationElementCollection siteCollection = siteElement.GetCollection();
ConfigurationElement applicationElement = siteCollection.CreateElement("application");
applicationElement["path"] = @"/";
ConfigurationElementCollection applicationCollection = applicationElement.GetCollection();
ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory");
virtualDirectoryElement["path"] = @"/";
virtualDirectoryElement["physicalPath"] = @"C:\Inetpub\www.contoso.com\wwwroot";
applicationCollection.Add(virtualDirectoryElement);
siteCollection.Add(applicationElement);
sitesCollection.Add(siteElement);
serverManager.CommitChanges();
}
}
}
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 = sitesCollection.CreateElement("site")
siteElement("name") = "Contoso"
siteElement("id") = 2
siteElement("serverAutoStart") = True
Dim bindingsCollection As ConfigurationElementCollection = siteElement.GetCollection("bindings")
Dim bindingElement As ConfigurationElement = bindingsCollection.CreateElement("binding")
bindingElement("protocol") = "http"
bindingElement("bindingInformation") = "*:80:www.contoso.com"
bindingsCollection.Add(bindingElement)
Dim siteCollection As ConfigurationElementCollection = siteElement.GetCollection
Dim applicationElement As ConfigurationElement = siteCollection.CreateElement("application")
applicationElement("path") = "/"
Dim applicationCollection As ConfigurationElementCollection = applicationElement.GetCollection
Dim virtualDirectoryElement As ConfigurationElement = applicationCollection.CreateElement("virtualDirectory")
virtualDirectoryElement("path") = "/"
virtualDirectoryElement("physicalPath") = "C:\Inetpub\www.contoso.com\wwwroot"
applicationCollection.Add(virtualDirectoryElement)
siteCollection.Add(applicationElement)
sitesCollection.Add(siteElement)
serverManager.CommitChanges()
End Sub
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 siteElement = sitesCollection.CreateNewElement("site");
siteElement.Properties.Item("name").Value = "Contoso";
siteElement.Properties.Item("id").Value = 2;
siteElement.Properties.Item("serverAutoStart").Value = true;
var bindingsCollection = siteElement.ChildElements.Item("bindings").Collection;
var bindingElement = bindingsCollection.CreateNewElement("binding");
bindingElement.Properties.Item("protocol").Value = "http";
bindingElement.Properties.Item("bindingInformation").Value = "*:80:www.contoso.com";
bindingsCollection.AddElement(bindingElement);
var siteCollection = siteElement.Collection;
var applicationElement = siteCollection.CreateNewElement("application");
applicationElement.Properties.Item("path").Value = "/";
var applicationCollection = applicationElement.Collection;
var virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory");
virtualDirectoryElement.Properties.Item("path").Value = "/";
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\\Inetpub\\www.contoso.com\\wwwroot";
applicationCollection.AddElement(virtualDirectoryElement);
siteCollection.AddElement(applicationElement);
sitesCollection.AddElement(siteElement);
adminManager.CommitChanges();
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
Set siteElement = sitesCollection.CreateNewElement("site")
siteElement.Properties.Item("name").Value = "Contoso"
siteElement.Properties.Item("id").Value = 2
siteElement.Properties.Item("serverAutoStart").Value = True
Set bindingsCollection = siteElement.ChildElements.Item("bindings").Collection
Set bindingElement = bindingsCollection.CreateNewElement("binding")
bindingElement.Properties.Item("protocol").Value = "http"
bindingElement.Properties.Item("bindingInformation").Value = "*:80:www.contoso.com"
bindingsCollection.AddElement bindingElement
Set siteCollection = siteElement.Collection
Set applicationElement = siteCollection.CreateNewElement("application")
applicationElement.Properties.Item("path").Value = "/"
Set applicationCollection = applicationElement.Collection
Set virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory")
virtualDirectoryElement.Properties.Item("path").Value = "/"
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\Inetpub\www.contoso.com\wwwroot"
applicationCollection.AddElement(virtualDirectoryElement)
siteCollection.AddElement applicationElement
sitesCollection.AddElement siteElement
adminManager.CommitChanges()