<Associazioni>

Panoramica

L'elemento <bindings> configura le informazioni di associazione per un sito Web IIS 7 o versione successiva. Può anche definire le associazioni predefinite per tutti i siti nel server Web, se incluso nell'elemento <siteDefaults> .

Questo elemento può contenere una raccolta di <binding> elementi. Ogni elemento della raccolta definisce un set separato di informazioni di associazione che una richiesta può usare per contattare il sito Web. Ad esempio, se il sito richiede agli utenti di contattarlo usando sia il protocollo HTTP che il protocollo HTTPS, è necessario definire un'associazione per ogni protocollo.

È anche possibile usare <clear /> l'elemento nell'elemento per eseguire l'override delle impostazioni predefinite dell'associazione <bindings> ereditate dall'elemento a livello <siteDefaults> di server.

Compatibilità

Versione Note
IIS 10.0 L'elemento <bindings> non è stato modificato in IIS 10.0.
IIS 8,5 L'elemento <bindings> non è stato modificato in IIS 8.5.
IIS 8,0 L'elemento <bindings> non è stato modificato in IIS 8.0.
IIS 7,5 L'elemento <bindings> non è stato modificato in IIS 7.5.
IIS 7.0 L'elemento <bindings> è stato introdotto in IIS 7.0.
IIS 6.0 L'insieme <bindings> sostituisce le sezioni della proprietà ServerBindings nell'oggetto metabase IIS 6.0 IIsWebServer .

Installazione

L'elemento <bindings> è incluso nell'installazione predefinita di IIS 7 o versione successiva.

Procedure

Come aggiungere informazioni di associazione a un sito

  1. Aprire Gestione Internet Information Services (IIS):

    • Se si usa Windows Server 2012 o Windows Server 2012 R2:

      • Sulla barra delle applicazioni fare clic su Server Manager, scegliere Strumenti, quindi fare clic su Gestione Internet Information Services (IIS).
    • Se si usa Windows 8 o Windows 8.1:

      • Tenere premuto il tasto Windows, premere la lettera X e quindi fare clic su Pannello di controllo.
      • Fare clic su Strumenti di amministrazione, quindi fare doppio clic su Gestione Internet Information Services (IIS).
    • Se si usa Windows Server 2008 o Windows Server 2008 R2:

      • Sulla barra delle applicazioni fare clic su Start, scegliere Strumenti di amministrazione, quindi fare clic su Gestione Internet Information Services (IIS).
    • Se si usa Windows Vista o Windows 7:

      • Sulla barra delle applicazioni fare clic su Start e quindi su Pannello di controllo.
      • Fare doppio clic su Strumenti di amministrazione, quindi fare doppio clic su Gestione Internet Information Services (IIS).
  2. Nel riquadro Connessioni espandere il nome del server, espandere Siti e quindi fare clic sul sito Web in cui si desidera configurare le associazioni.

  3. Nel riquadro Azioni fare clic su Associazioni...

  4. Nella finestra di dialogo Associazioni sito fare clic su Aggiungi...
    Screenshot della finestra di dialogo Associazioni sito che visualizza la porta H T T P 80.

  5. Nella finestra di dialogo Aggiungi associazione sito aggiungere le informazioni di associazione e quindi fare clic su OK.
    Screenshot della finestra di dialogo Aggiungi associazione sito. H T T P viene visualizzato nel campo Tipo.

Configurazione

È possibile aggiungere un <bindings> elemento per ogni sito nel file ApplicationHost.config, che può contenere una raccolta di singoli <binding> elementi che definiscono le singole associazioni di protocollo per il sito. Ogni sito dovrà avere almeno un'associazione HTTP o HTTPS per essere visualizzabile tramite Internet.

È anche possibile usare <clear /> l'elemento nell'elemento per eseguire l'override delle impostazioni predefinite dell'associazione <bindings> ereditate dall'elemento a livello <siteDefaults> di server.

Attributi

Nessuno.

Elementi figlio

Elemento Descrizione
binding Elemento facoltativo.

Configura un'associazione nel sito padre.
clear Elemento facoltativo.

Cancella i riferimenti alle impostazioni predefinite ereditate dal livello padre della configurazione.

Esempio di configurazione

L'esempio seguente definisce un sito denominato Contoso con due associazioni. La prima associazione è per un nome host "www.contoso.com" sulla porta 80 per l'indirizzo IP 192.168.0.1 e la seconda è per un'associazione HTTPS per tutti gli indirizzi IP sulla porta 443.

<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>

Codice di esempio

Gli esempi seguenti configurano un sito denominato Contoso con un nome host "www.contoso.com" sulla porta 80 per l'indirizzo IP 192.168.0.1 e un'associazione HTTPS per tutti gli indirizzi IP sulla porta 443.

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:']

In alternativa, è possibile usare:

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

Nota

È necessario assicurarsi di impostare il parametro commit su apphost quando si usa AppCmd.exe per configurare queste impostazioni. In questo modo le impostazioni di configurazione vengono confermate nella sezione relativa al percorso appropriato nel file 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