I have a single site that has many names. I want to be able to programatically add a new host header record to IIS to allow it to recognize another name. Specifically, what is the code (preferably in C#) to add a new host header to a given site?
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With