代码之家  ›  专栏  ›  技术社区  ›  Jonathan Sternberg

以编程方式设置IIS 6.0的服务器绑定

  •  2
  • Jonathan Sternberg  · 技术社区  · 14 年前

    我正在尝试设置一个安装程序来注册一个网站。目前,我已经在WindowsServer2003下创建了一个应用程序池和网站。不幸的是,每当我试图修改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。我检查过了,服务器绑定的大小是0。当我试图在列表中创建一个新条目时,如下所示:

    siteRootDE.Properties.Item("ServerBindings").Add("<address>")
    

    当我试着那样做的时候,我得到了一个例外。

    我查看了注册的属性键,没有找到ServerBindings。但是,当我通过IIS创建网站时,它会正确地生成服务器绑定,我可以看到它。

    编辑:我把代码移到了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();
    
    1 回复  |  直到 14 年前
        1
  •  5
  •   Garett    14 年前

    在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