문제

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

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top