我有一个有很多名字的网站.我希望能够以编程方式向IIS添加新的主机头记录,以允许它识别另一个名称.具体来说,将新主机头添加到给定站点的代码(最好是在C#中)是什么?
static void Main(string[] args) { AddHostHeader(1, "127.0.0.1", 8080, "fred"); AddHostHeader(1, null, 8081, null); } 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(); } }