开发者

Change http port for "Default Web Site"

开发者 https://www.devze.com 2023-03-08 06:51 出处:网络
I am using Microsoft.Web.Adminsitration assembly to create Application pool and website. Refer开发者_StackOverflow社区ing to:

I am using Microsoft.Web.Adminsitration assembly to create Application pool and website.

Refer开发者_StackOverflow社区ing to:

Microsoft.Web.Administration in IIS 7

This custom website should have port 80 for http communication. Since "Default Web Site" also uses port 80 by default, I need to programatically change this to another port (say 90). Is there a way of doing this programatically?


Here's a sample snippet of code to change the existing port 80 http binding on the Default Web site 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("");
  }
}

A BindingInformation field is a string comprised of:

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

For example:

  • *:80: - listen 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 - listen on ip address 172.16.3.1, port 80 and if host header is example.com


The second line up "Creating a Site" in the blog post you mentioned shows you how ...

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

The parameter *:8080 says the website should listen on all IP address bound to the machine on Port 8080. Just change 8080 to the port you want.

FYI, 8080 is a traditional "off port" location for web sites. Just keep in mind that you won't want to conflict with other services that are using ports. You can see a list of reserved/registered ports here :http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

0

精彩评论

暂无评论...
验证码 换一张
取 消