绑定 <bindings>

概述

<bindings> 元素可为 IIS 7 或更高版本的网站配置绑定信息。 如果它包含在 <siteDefaults> 元素中,还可以为 Web 服务器上的所有站点定义默认绑定。

此元素可以包含 <binding> 元素集合。 集合中的每个元素都定义一组单独的绑定信息,请求可使用这些信息来联系网站。 例如,如果你的站点要求用户同时使用 HTTP 协议和 HTTPS 协议进行联系,则必须为每个协议定义一个绑定。

还可以在 <bindings> 元素中使用 <clear /> 元素来覆盖从服务器级别 <siteDefaults> 元素继承的绑定默认值。

兼容性

版本 说明
IIS 10.0 <bindings> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <bindings> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <bindings> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <bindings> 元素在 IIS 7.5 中未进行修改。
IIS 7.0 <bindings> 元素是在 IIS 7.0 中引入的。
IIS 6.0 <bindings> 集合取代了 IIS 6.0 IIsWebServer 元数据库对象上 ServerBindings 属性的部分

安装

在 IIS 7 或更高版本的默认安装中包含 <bindings> 元素。

操作方式

如何为站点添加绑定信息

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”
      • 单击“管理工具”,然后双击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”
      • 双击“管理工具”,然后双击“Internet Information Services (IIS)管理器”
  2. 在“连接”窗格中,展开服务器名称,展开“网站”,然后单击要在其中配置绑定的网站

  3. 在“操作”窗格中,单击“绑定...”

  4. 在“站点绑定”对话框中,单击“添加...”
    Screenshot of the Site Bindings dialog box displaying H T T P port 80.

  5. 在“添加站点绑定”对话框中,添加绑定信息,然后单击“确定”
    Screenshot of the Add Site Binding dialog box. H T T P is displayed in the Type field.

配置

可以在 ApplicationHost.config 文件中为每个站点添加一个 <bindings> 元素,其中可包含一组单独的 <binding> 元素,用于定义站点的各个协议绑定。 每个站点至少需要一个 HTTP 或 HTTPS 绑定才能通过 Internet 进行查看。

还可以在 <bindings> 元素中使用 <clear /> 元素来覆盖从服务器级别 <siteDefaults> 元素继承的绑定默认值。

特性

无。

子元素

元素 说明
binding 可选元素。

在父站点中配置绑定。
clear 可选元素。

清除对从配置的父级别继承的默认设置的引用。

配置示例

以下示例为名为 Contoso 的站点定义了两个绑定。 第一个绑定指定主机名为“www.contoso.com”,端口为 80,IP 地址为 192.168.0.1,第二个绑定指定通过端口 443 对所有 IP 地址使用 HTTPS 绑定。

<site name="Contoso" id="2">
   <application path="/" applicationPool="Contoso">
      <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot" />
   </application>
   <bindings>
      <binding protocol="http" bindingInformation="192.168.0.1:80:www.contoso.com" />
      <binding protocol="https" bindingInformation="*:443:" />
   </bindings>
</site>

代码示例

以下示例配置了一个名为 Contoso 的站点,其主机名为“www.contoso.com”,端口为 80,IP 地址为 192.168.0.1,并通过端口 443 对所有 IP 地址使用 HTTPS 绑定。

AppCmd.exe

appcmd.exe set site /site.name:Contoso /+bindings.[protocol='http',bindingInformation='192.168.0.1:80:www.contoso.com']

appcmd.exe set site /site.name:Contoso /+bindings.[protocol='https',bindingInformation='*:443:']

或者,可以使用:

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].bindings.[protocol='http',bindingInformation='192.168.0.1:80:www.contoso.com']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].bindings.[protocol='https',bindingInformation='*:443:']" /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

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 bindingsCollection = siteElement.GetCollection("bindings");
          ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
          bindingElement["protocol"] = @"http";
          bindingElement["bindingInformation"] = @"192.168.0.1:80:www.contoso.com";
          bindingsCollection.Add(bindingElement);

          ConfigurationElement bindingElement1 = bindingsCollection.CreateElement("binding");
          bindingElement1["protocol"] = @"https";
          bindingElement1["bindingInformation"] = @"*:443:";
          bindingsCollection.Add(bindingElement1);

          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 bindingsCollection As ConfigurationElementCollection = siteElement.GetCollection("bindings")

      Dim bindingElement As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement("protocol") = "http"
      bindingElement("bindingInformation") = "192.168.0.1:80:www.contoso.com"
      bindingsCollection.Add(bindingElement)

      Dim bindingElement1 As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement1("protocol") = "https"
      bindingElement1("bindingInformation") = "*:443:"
      bindingsCollection.Add(bindingElement1)

      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 bindingsCollection = siteElement.ChildElements.Item("bindings").Collection;

var bindingElement = bindingsCollection.CreateNewElement("binding");
bindingElement.Properties.Item("protocol").Value = "http";
bindingElement.Properties.Item("bindingInformation").Value = "192.168.0.1:80:www.contoso.com";
bindingsCollection.AddElement(bindingElement);

var bindingElement1 = bindingsCollection.CreateNewElement("binding");
bindingElement1.Properties.Item("protocol").Value = "https";
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:";
bindingsCollection.AddElement(bindingElement1);

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 bindingsCollection = siteElement.ChildElements.Item("bindings").Collection

Set bindingElement = bindingsCollection.CreateNewElement("binding")
bindingElement.Properties.Item("protocol").Value = "http"
bindingElement.Properties.Item("bindingInformation").Value = "192.168.0.1:80:www.contoso.com"
bindingsCollection.AddElement(bindingElement)

Set bindingElement1 = bindingsCollection.CreateNewElement("binding")
bindingElement1.Properties.Item("protocol").Value = "https"
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:"
bindingsCollection.AddElement(bindingElement1)

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