SiteCollection.Add Method

Definition

Adds a new Web site to the site collection.

Overloads

Add(String, String, Int32)

Adds a new Web site to the site collection by using the specified site name, path, and binding port.

Add(String, String, String, Byte[])

Adds a new Web site to the site collection by using the specified site name, binding information, physical file path, and certificate hash.

Add(String, String, String, String)

Creates a new site by using the specified parameters and adds it to the site collection.

Add(String, String, String, Byte[], String)
Add(String, String, String, Byte[], String, SslFlags)

Add(String, String, Int32)

Adds a new Web site to the site collection by using the specified site name, path, and binding port.

public:
 Microsoft::Web::Administration::Site ^ Add(System::String ^ name, System::String ^ physicalPath, int port);
public Microsoft.Web.Administration.Site Add (string name, string physicalPath, int port);
override this.Add : string * string * int -> Microsoft.Web.Administration.Site
Public Function Add (name As String, physicalPath As String, port As Integer) As Site

Parameters

name
String

The name of the new Web site.

physicalPath
String

The physical file path of the new Web site.

port
Int32

The listening port of the new Web site.

Returns

The Site object that was added to the SiteCollection object.

Remarks

The Site object is added to the collection in memory during this call. However, to commit the site configuration to the configuration system, you will need to use the ServerManager class to perform the update.

Applies to

Add(String, String, String, Byte[])

Adds a new Web site to the site collection by using the specified site name, binding information, physical file path, and certificate hash.

public:
 Microsoft::Web::Administration::Site ^ Add(System::String ^ name, System::String ^ bindingInformation, System::String ^ physicalPath, cli::array <System::Byte> ^ certificateHash);
public Microsoft.Web.Administration.Site Add (string name, string bindingInformation, string physicalPath, byte[] certificateHash);
override this.Add : string * string * string * byte[] -> Microsoft.Web.Administration.Site
Public Function Add (name As String, bindingInformation As String, physicalPath As String, certificateHash As Byte()) As Site

Parameters

name
String

The name of the new Web site.

bindingInformation
String

The binding information for the new Web site.

physicalPath
String

The physical file path of the new Web site.

certificateHash
Byte[]

The certificate hash that the new site should use for Secure Sockets Layer (SSL).

Returns

The Site object that was added to the SiteCollection object.

Remarks

The site binding information is a string that contains the protocol, IP address, TCP port, and host header. For example, a Web site that is listening for HTTP connections on port 80 for all IP addresses on the system would use "http:*:80:" for the bindingInformation parameter. Note that the asterisk (*) is a wildcard character indicating that the site should listen on all IP addresses. In this example, the host header was omitted. The host header can contain the domain name of the Web site and is used when more than one Web site is bound to the same IP address. For example, a Web site that is listening for HTTP connections on port 80 of IP address 192.168.1.100 with a domain name of "microsoft.com" would use "http:192.168.1.100:80:microsoft.com" for the bindingInformation parameter.

The Site object is added to the collection in memory during this call. However, to commit the site configuration to the configuration system, you will need to use the ServerManager class to perform the update.

Applies to

Add(String, String, String, String)

Creates a new site by using the specified parameters and adds it to the site collection.

public:
 Microsoft::Web::Administration::Site ^ Add(System::String ^ name, System::String ^ bindingProtocol, System::String ^ bindingInformation, System::String ^ physicalPath);
public Microsoft.Web.Administration.Site Add (string name, string bindingProtocol, string bindingInformation, string physicalPath);
override this.Add : string * string * string * string -> Microsoft.Web.Administration.Site
Public Function Add (name As String, bindingProtocol As String, bindingInformation As String, physicalPath As String) As Site

Parameters

name
String

The name to assign to the site (for example, "Default Web Site").

bindingProtocol
String

A comma-delimited list of protocols that the new site should use.

bindingInformation
String

A colon-delimited set of data that indicates the IP address, port, and host headers that the site listener should be bound to.

physicalPath
String

The physical location of the content that this site will provide access to.

Returns

A reference to the newly created Site object.

Exceptions

The name parameter that is passed to the method is null or is a zero-length string.

Examples

The following example creates a site and updates the configuration system.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;
using Microsoft.Web.Management;

namespace AdministrationSnippets
{
    class MicrosoftWebAdministrationSite
    {// Creates an site named HRWeb
        public void CreateSite()
        {
            CreateSiteByName("HRWeb");
        }

        // Creates a new site with the specified name
        public void CreateSiteByName(string name)
        {
            string path = @"C:\inetpub\" + name + "site";

            // Validate the site name
            char[] invalid = SiteCollection.InvalidSiteNameCharacters();
            if (name.IndexOfAny(invalid) > -1)
            {
                Console.WriteLine("Invalid site name: {0}", name);
            }

            // Create the content directory if it doesn't exist.
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }

            // Create a new site using the new directory and update the config
            ServerManager manager = new ServerManager();
            try
            {   // Add this site.
                Site hrSite = manager.Sites.Add(name, "http", "*:9090:", path);
                // The site will need to be started manually.
                hrSite.ServerAutoStart = false;
                manager.CommitChanges();
                Console.WriteLine("Site " + name + " added to ApplicationHost.config file.");
            }
            catch
            {
                // A site with this binding already exists. Do not attempt
                // to add a duplicate site.
            }
        }
    }
}

Remarks

This method creates a Site object and adds it to the SiteCollection object in memory. However, to commit the site configuration to the configuration system, you will need to use the ServerManager class to perform an update. The instance returned from this call does not reflect the default site values specified in the SiteDefaults property of the ServerManager object.

Applies to

Add(String, String, String, Byte[], String)

public:
 Microsoft::Web::Administration::Site ^ Add(System::String ^ name, System::String ^ bindingInformation, System::String ^ physicalPath, cli::array <System::Byte> ^ certificateHash, System::String ^ certificateStore);
public Microsoft.Web.Administration.Site Add (string name, string bindingInformation, string physicalPath, byte[] certificateHash, string certificateStore);
override this.Add : string * string * string * byte[] * string -> Microsoft.Web.Administration.Site
Public Function Add (name As String, bindingInformation As String, physicalPath As String, certificateHash As Byte(), certificateStore As String) As Site

Parameters

name
String
bindingInformation
String
physicalPath
String
certificateHash
Byte[]
certificateStore
String

Returns

Applies to

Add(String, String, String, Byte[], String, SslFlags)

public:
 Microsoft::Web::Administration::Site ^ Add(System::String ^ name, System::String ^ bindingInformation, System::String ^ physicalPath, cli::array <System::Byte> ^ certificateHash, System::String ^ certificateStore, Microsoft::Web::Administration::SslFlags sslFlags);
public Microsoft.Web.Administration.Site Add (string name, string bindingInformation, string physicalPath, byte[] certificateHash, string certificateStore, Microsoft.Web.Administration.SslFlags sslFlags);
override this.Add : string * string * string * byte[] * string * Microsoft.Web.Administration.SslFlags -> Microsoft.Web.Administration.Site

Parameters

name
String
bindingInformation
String
physicalPath
String
certificateHash
Byte[]
certificateStore
String
sslFlags
SslFlags

Returns

Applies to