2012-02-11 39 views
2

我想一個主機頭添加到一個網站,該網站正在通過IIS7 Web應用程序(asp.net 4.0/C#),但在互聯網的一些例子,但是我想他們中的大多數人不在iis7上工作。 (注:Web應用程序在同一臺服務器被託管,因此我猜不會是一個安全問題,同時改變IIS配置)主機頭添加到網站上的IIS 7編程

任何幫助表示讚賞,感謝

+0

這個問題可能會幫助你http://stackoverflow.com/questions/5121383/iis-7-0-vs-7-5-site-microsoft-web-administration-site -bindingcollection – 2012-02-11 15:16:28

+0

我覺得你不夠清楚。你能提供更多細節嗎?你想添加/更改綁定? – Tomek 2012-02-11 15:17:25

回答

2

我發現這個解決方案,它爲我工作。這是與夫婦參數的小功能,只是你必須找到yourwebsite在你的ISS configuration.After的ID,你必須給服務器(IIS)的IP地址和端口號和主機名的功能和它將通過使用您輸入的參數添加主機頭。例如,

AddHostHeader(2,「127.0.0.1:81」, 81,「newsHostHeader」);

static void AddHostHeader(int? websiteID, string ipAddress, int? port, string hostname) 
    { 
     using (var directoryEntry = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID.ToString())) 
     { 
      var bindings = directoryEntry.Properties["ServerBindings"]; 
      var header = string.Format("{0}:{1}:{2}", ipAddress, port, hostname); 
      if (bindings.Contains(header)) 
       throw new InvalidOperationException("Host Header already exists!"); 
      bindings.Add(header); 
      directoryEntry.CommitChanges(); 
     } 
    } 

(注意:不要忘了添加到使用 的System.DirectoryServices的頁面;利用Microsoft.Web.Administration;)

+0

拋出在System.DirectoryServices.DirectoryEntry.Bind 在Windows 8 IIS 7.5 System.Runtime.InteropServices.COMException發生 – 2013-03-09 08:29:59

+0

也許你需要檢查一些憑據設置,聽起來像不允許IUSR用戶添加標題的網站在你的IIS中。 – slayer35 2013-03-09 15:00:45

+0

犯錯..我eventualy用它來完成它 http://www.iis.net/configreference/system.applicationhost/sites/site/bindings/binding – 2013-03-09 16:44:23