更改“默认网站”的 http 端口

发布于 2024-11-09 06:25:53 字数 592 浏览 0 评论 0原文

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

Refering 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-11-16 06:25:53

下面是一个示例代码片段,用于将默认网站上现有的端口 80 http 绑定更改为端口 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 字段是一个由

以下部分组成的字符串: :<; 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

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
何处潇湘 2024-11-16 06:25:53

您提到的博客文章中的第二行“创建站点”向您展示了如何...

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

参数*:8080表示网站应该侦听绑定到上的计算机的所有IP地址端口 8080。只需将 8080 更改为您想要的端口即可。

仅供参考,8080 是网站的传统“端口外”位置。请记住,您不想与使用端口的其他服务发生冲突。您可以在此处查看保留/注册端口的列表:http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文