我正在尝试设置安装程序来注册网站。目前,我已经在 Windows Server 2003 下创建了应用程序池和网站。不幸的是,每当我尝试修改 ServerBindings 属性来设置 IP 地址时,它都会向我抛出异常。我第一次尝试这个是因为这里的文档告诉我 http://msdn.microsoft.com/en-us/library/ms525712%28VS.90%29.aspx 。我目前正在使用 VB.NET,但 C# 答案也可以,因为无论如何我都需要将其切换为使用 C#。
siteRootDE.Properties.Item("ServerBindings").Item(0) = "<address>"
这会引发 ArgumentOutOfRangeException。我检查了一下,服务器绑定(bind)的大小为 0。当我尝试在列表中创建一个新条目时,如下所示:
siteRootDE.Properties.Item("ServerBindings").Add("<address>")
当我尝试这样做时,我得到了一个 COMException。
我查看了注册的属性键,却找不到 ServerBindings。但是,当我通过 IIS 创建网站时,它会正确生成 ServerBindings,并且我可以看到它。
我需要做什么才能让 ServerBindings 出现?
编辑:我将代码移至 C# 并尝试了。似乎出于某种原因,VB.NET 在出现上述任一情况时会崩溃,但 C# 不会。但该代码似乎仍然没有做任何事情。它只是默默地失败了。我正在尝试这样:
// WebPage is the folder where I created the website
DirectoryEntry siteRootDE = new DirectoryRoot("IIS://LocalHost/W3SVC/WebPage");
// www.mydomain.com is one of the IP addresses that shows up
// when I used the IIS administrative program
siteRootDE.Properties["ServerBindings"].Value = ":80:www.mydomain.com";
siteRootDE.CommitChanges();
最佳答案
在 C# 中,您应该能够执行以下操作:
webSite.Invoke("Put", "ServerBindings", ":80:www.mydomain.com");
或
webSite.Properties["ServerBindings"].Value = ":80:www.mydomain.com";
编辑:
这是我使用的示例代码。
public static void CreateNewWebSite(string siteID, string hostname)
{
DirectoryEntry webService = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
DirectoryEntry website = new DirectoryEntry();
website = webService.Children.Add(siteID, "IIsWebServer");
website.CommitChanges();
website.Invoke("Put", "ServerBindings", ":80:" + hostname);
// Or website.Properties["ServerBindings"].Value = ":80:" + hostname;
website.Properties["ServerState"].Value = 2;
website.Properties["ServerComment"].Value = hostname;
website.CommitChanges();
DirectoryEntry rootDir = website.Children.Add("ROOT", "IIsWebVirtualDir");
rootDir.CommitChanges();
rootDir.Properties["AppIsolated"].Value = 2;
rootDir.Properties["Path"].Value = @"C:\Inetpub\wwwroot\MyRootDir";
rootDir.Properties["AuthFlags"].Value = 5;
rootDir.Properties["AccessFlags"].Value = 513;
rootDir.CommitChanges();
website.CommitChanges();
webService.CommitChanges();
}
另外,这里有一个很好的article供引用。
关于c# - 以编程方式设置 IIS 6.0 的服务器绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3321927/