更改“默认网站"的http端口; [英] Change http port for "Default Web Site"
问题描述
我正在使用Microsoft.Web.Adminsitration程序集创建应用程序池和网站.
I am using Microsoft.Web.Adminsitration assembly to create Application pool and website.
引用:
此自定义网站应具有用于HTTP通讯的端口80.由于默认网站"默认情况下也使用端口80,因此我需要以编程方式将其更改为另一个端口(例如90).有办法以编程方式执行此操作吗?
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?
推荐答案
下面是示例代码片段,用于将默认网站上的现有端口80 http绑定更改为端口90:
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("");
}
}
BindingInformation
字段是一个字符串,包含:
A BindingInformation
field is a string comprised of:
< ip地址>:< port>:<主机头>
例如:
-
*:80:
-侦听所有IP地址,端口80,无主机头 -
*:90:example.com
-侦听所有IP地址,端口80,但如果主机标头与example.com
匹配则进行响应 -
172.16.3.1:80:example.com
-侦听IP地址172.16.3.1,端口80,以及主机头是否为example.com
*: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 matchesexample.com
172.16.3.1:80:example.com
- listen on ip address 172.16.3.1, port 80 and if host header isexample.com
这篇关于更改“默认网站"的http端口;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!