バインド <bindings>
- 概要
- 互換性
- セットアップ
- 方法
- 構成
- サンプル コード
※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。
概要
<bindings>
要素は、IIS 7.0 Web サイトのバインド情報を構成します。また、<siteDefaults>
要素に含めた場合は、Web サーバー上のすべてのサイトの既定のバインドを定義することもできます。
この要素は、<binding>
要素のコレクションを含むことができます。コレクション内の各要素は、要求が Web サイトと通信するために使用するバインド情報の個別のセットを定義します。たとえば、ユーザーがサイトと通信する際に HTTP と HTTPS の両プロトコルを使用する必要がある場合、それぞれのプロトコルに対してバインドを定義する必要があります。
また、<bindings>
要素の <clear />
要素を使用して、サーバー レベルの <siteDefaults>
要素から継承されたバインドの既定値を無効にすることもできます。
互換性
IIS 7.0 | IIS 6.0 | |
---|---|---|
注意 | <bindings> は IIS 7.0 で新たに導入された要素です。 |
|
セットアップ
<bindings>
要素は、IIS 7.0 の既定のインストールに含まれています。
方法
サイトへのバインド情報の追加方法
タスク バーで [スタート] をクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
[接続] ウィンドウで当該サーバー名を展開して [サイト] を展開し、バインドを構成する Web サイトをクリックします。
[操作] ウィンドウで [バインド] をクリックします。
[サイト バインド] ダイアログ ボックスで、[追加] をクリックします。
[サイト バインドの追加] ダイアログ ボックスで、バインド情報を追加し、[OK] をクリックします。
構成
<bindings>
要素は、ApplicationHost.config ファイルの各サイトに対して追加でき、サイトのそれぞれのプロトコル バインドを定義する個々の<binding>
要素のコレクションを含めることができます。各サイトをインターネット上で表示できるようにするには、1 つ以上の HTTP または HTTPS バインドを構成する必要があります。
また、<bindings>
要素の <clear />
要素を使用して、サーバー レベルの <siteDefaults>
要素から継承されたバインドの既定値を無効にすることもできます。
属性
なし。
子要素
要素 | 説明 |
---|---|
binding |
オプションの要素。 親サイトにバインドを構成します。 |
clear |
オプションの要素。 構成の親レベルから継承された既定の設定への参照を消去します。 |
構成サンプル
次の例では、2 つのバインドが構成された Contoso という名前のサイトを定義します。1 つ目は、ホスト名 "www.contoso.com" を使用したポート 80 上の IP アドレス 192.168.0.1 に対するバインド、2 つ目は、ポート 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:443: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
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 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