2009-04-23 3 views
6

많은 이름을 가진 단일 사이트가 있습니다. 나는 새로운 호스트 헤더 레코드를 IIS에 프로그램 적으로 추가하여 다른 이름을 인식 할 수 있기를 원합니다. 특히, 주어진 사이트에 새로운 호스트 헤더를 추가하는 코드는 무엇입니까 (C# 사용 가능)?IIS 웹 사이트에 호스트 헤더를 추가하는 코드

+0

자세한 답변을 아래의 코드를합니까 개체 사용? – andleer

답변

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

사용로 가장 또는 Windows 신원이

관련 문제