Question

I want to add a host header to a website which is working on IIS7 through a web application (asp.net 4.0 / C#).There are some examples in internet,but i guess most of them dont work on iis7. (note:the web application is being hosted in same server so i guess there wont be a security problem while changing iis configurations)

Any help is appreciated,thanks

Was it helpful?

Solution

I found this solution and it works for me.This is a little function with couple parameters,just you have to find the id of yourwebsite in your iss configuration.After that you have to give the ip adress of the server (iis),and port number,and hostname to the function and it will add a hostheader by using the parameters you entered.For example

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();
        }
    }

(note:do not forget to add to the page using System.DirectoryServices; using Microsoft.Web.Administration; )

OTHER TIPS

The above solution didn't quite work with IIS7.5 for me. I eventually had to do this http://www.iis.net/configreference/system.applicationhost/sites/site/bindings/binding

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top