Change http port for "Default Website"

I am using the Microsoft.Web.Adminsitration assembly to create an application pool and website.

Referring to:

Microsoft.Web.Administration in IIS 7

This user website must have port 80 in order to communicate with http. Since the “Default Web Site” also uses port 80 by default, I need to programmatically change it to another port (say 90). Is there any way to do this programmatically?

0
source share
2 answers

Here is an example code snippet for changing an existing http binding port 80 on the default website to port 90:

int iisNumber = 1; // Default Website is IIS#1 using(ServerManager serverManager = new ServerManager()) { Site site = serverManager.Sites.FirstOrDefault(s => s.Id == iisNumber); if(site != null) { Binding binding = site.Bindings .Where(b => b.BindingInformation == "*:80:" && b.Protocol == "http") .FirstOrDefault(); binding.BindingInformation = "*:90:"; serverManager.CommitChanges(); Console.WriteLine(""); } } 
Field

A BindingInformation is a string consisting of:

<ip address>:<port>:<host header>

For instance:

  • *:80: - listening on all IP addresses, port 80, no host header
  • *:90:example.com - listen on all IP addresses, port 80, but respond if the host header matches example.com
  • 172.16.3.1:80:example.com - listening on the IP address 172.16.3.1, port 80, and if the host header is example.com
+3
source

The second line of "Creating a site" in your blog post shows you how ...

 iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite"); 

Parameter *:8080 says that the website should listen on all IP addresses bound to the machine on Port 8080 . Just change the 8080 to the desired port.

FYI, 8080 is the traditional "off port" location for websites. Just keep in mind that you do not want to conflict with other services using ports. Here you can see the list of reserved / registered ports: http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

0
source

All Articles