我们已经能够创建一个网站.我们使用此链接中的信息执行此操作:
https://msdn.microsoft.com/en-us/library/ms525598.aspx
但是,我们想使用端口号80以外的端口号.我们如何做到这一点?
我们正在使用IIS 6
如果您使用的是IIS 7,则会有一个名为Microsoft.Web.Administration的新托管API
以上博客文章的一个例子:
ServerManager iisManager = new ServerManager(); iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite"); iisManager.CommitChanges();
如果您正在使用IIS 6并且想要这样做,那么遗憾的是它更复杂.
您必须在每台服务器上创建一个Web服务,这是一个处理网站创建的Web服务,因为通过网络直接用户模拟将无法正常工作(如果我没记错的话).
您将不得不使用Interop Services并执行与此类似的操作(此示例使用两个对象,server和site,它们是存储服务器和站点配置的自定义类的实例):
string metabasePath = "IIS://" + server.ComputerName + "/W3SVC"; DirectoryEntry w3svc = new DirectoryEntry(metabasePath, server.Username, server.Password); string serverBindings = ":80:" + site.HostName; string homeDirectory = server.WWWRootPath + "\\" + site.FolderName; object[] newSite = new object[] { site.Name, new object[] { serverBindings }, homeDirectory }; object websiteId = (object)w3svc.Invoke("CreateNewSite", newSite); // Returns the Website ID from the Metabase int id = (int)websiteId;
在这里查看更多
继承人解决方案.
博客文章:如何在IIS 7中添加新网站
按钮点击:
try { ServerManager serverMgr = new ServerManager(); string strWebsitename = txtwebsitename.Text; // abc string strApplicationPool = "DefaultAppPool"; // set your deafultpool :4.0 in IIS string strhostname = txthostname.Text; //abc.com string stripaddress = txtipaddress.Text;// ip address string bindinginfo = stripaddress + ":80:" + strhostname; //check if website name already exists in IIS Boolean bWebsite = IsWebsiteExists(strWebsitename); if (!bWebsite) { Site mySite = serverMgr.Sites.Add(strWebsitename.ToString(), "http", bindinginfo, "C:\\inetpub\\wwwroot\\yourWebsite"); mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool; mySite.TraceFailedRequestsLogging.Enabled = true; mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site"; serverMgr.CommitChanges(); lblmsg.Text = "New website " + strWebsitename + " added sucessfully"; } else { lblmsg.Text = "Name should be unique, " + strWebsitename + " is already exists. "; } } catch (Exception ae) { Response.Redirect(ae.Message); }
在名称已存在的网站上循环
public bool IsWebsiteExists(string strWebsitename) { Boolean flagset = false; SiteCollection sitecollection = serverMgr.Sites; foreach (Site site in sitecollection) { if (site.Name == strWebsitename.ToString()) { flagset = true; break; } else { flagset = false; } } return flagset; }