代码之家  ›  专栏  ›  技术社区  ›  Shiraz Bhaiji

使用C以编程方式在IIS中创建网站并设置端口号

  •  33
  • Shiraz Bhaiji  · 技术社区  · 15 年前

    我们已经能够创建一个网站。我们使用此链接中的信息执行此操作:

    https://msdn.microsoft.com/en-us/library/ms525598.aspx

    但是,我们希望使用端口80以外的端口号。我们该怎么做?

    我们正在使用IIS 6

    4 回复  |  直到 7 年前
        1
  •  72
  •   Community CDub    9 年前

    如果您使用的是IIS 7,则有一个名为 Microsoft.Web.Administration

    以上博客文章中的一个例子:

    ServerManager iisManager = new ServerManager();
    iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite");
    iisManager.CommitChanges(); 
    

    如果您使用的是IIS6并希望这样做,那么不幸的是,它更复杂。

    您必须在每台服务器上创建一个Web服务,这是一个处理网站创建的Web服务,因为通过网络进行的直接用户模拟无法正常工作(如果我正确地回忆起这一点)。

    您必须使用互操作服务并执行类似的操作(本例使用两个对象,即服务器和站点,它们是存储服务器和站点配置的自定义类的实例):

    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;
    

    查看更多 here

        2
  •  13
  •   Satinder singh Arun P Johny    10 年前

    这是解决方案。
    Blog article : How to add new website in 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;
        }
    
        3
  •  4
  •   Hari    15 年前

    尝试以下代码以了解未使用的端口号

            DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
    
            // Find unused ID PortNo for new web site
            bool found_valid_port_no = false;
            int random_port_no = 1;
    
            do
            {
                bool regenerate_port_no = false;
                System.Random random_generator = new Random();
                random_port_no = random_generator.Next(9000,15000);
    
                foreach (DirectoryEntry e in root.Children)
                {
                    if (e.SchemaClassName == "IIsWebServer")
                    {
    
                        int site_id = Convert.ToInt32(e.Name);
                        //For each detected ID find the port Number 
    
                         DirectoryEntry vRoot = new DirectoryEntry("IIS://localhost/W3SVC/" + site_id);
                         PropertyValueCollection pvcServerBindings = vRoot.Properties["serverbindings"];
                         String bindings = pvcServerBindings.Value.ToString().Replace(":", "");
                         int port_no = Convert.ToInt32(bindings);
    
                         if (port_no == random_port_no)
                        {
                            regenerate_port_no = true;
                            break;
                        }
                    }
                }
    
                found_valid_port_no = !regenerate_port_no;
            } while (!found_valid_port_no);
    
            int newportId = random_port_no;
    
        4
  •  0
  •   Dewfy    15 年前
    1. 在站点属性中,选择“Web站点”选项卡并指定TCP端口。
    2. 在Studio中调试目的指定 http://localhost:<port>/<site&gt ;at tab web for“使用本地IIS Web服务器”