我正在尝试设置安装程序来注册网站.目前,我已经在Windows Server 2003下创建了一个应用程序池和Web站点.不幸的是,每当我尝试修改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>"
Run Code Online (Sandbox Code Playgroud)
这会抛出ArgumentOutOfRangeException.我检查了它,服务器绑定的大小为0.当我尝试在列表中创建一个新条目时,如下所示:
siteRootDE.Properties.Item("ServerBindings").Add("<address>")
Run Code Online (Sandbox Code Playgroud)
我尝试的时候遇到了COMException.
我查看了已注册的属性键,无法找到ServerBindings.但是,当我通过IIS创建Web站点时,它会正确生成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();
Run Code Online (Sandbox Code Playgroud)
在C#中你应该能够这样做:
webSite.Invoke("Put", "ServerBindings", ":80:www.mydomain.com");
Run Code Online (Sandbox Code Playgroud)
要么
webSite.Properties["ServerBindings"].Value = ":80:www.mydomain.com";
Run Code Online (Sandbox Code Playgroud)
编辑:
这是我使用的示例代码.
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();
}
Run Code Online (Sandbox Code Playgroud)
另外,这里有一篇很好的文章供参考.
| 归档时间: |
|
| 查看次数: |
3172 次 |
| 最近记录: |