ASP.NET Core 6(.NET 6) 修改默认端口的方法(5000和5001)

ASP.NET Core 6(.NET 6) 默认将 HTTP 端口绑定到 5000,将 HTTPS 端口绑定到 5001。可以通过以下三种方式修改默认端口:

详细文档:ASP.NET Core 6(.NET 6) 修改默认端口的方法(5000和5001)-CJavaPy

方法一:修改 launchSettings.json 文件

在项目的根目录下,找到 launchSettings.json 文件。在文件中,找到 <environment name="Development"> 部分,并修改 <webHostUrl> 属性的值。

例如,要将 HTTP 端口修改为 80,请将 <webHostUrl> 属性的值修改为 http://localhost:80

JSON

{
  "profiles": {
    "ASPNETCore": {
      "commandName": "dotnet",
      "launchBrowser": true,
      "environments": {
        "Development": {
          "commandArgs": ["run", "--urls", "http://localhost:80"],
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
  }
}

方法二:修改 appsettings.json 文件

在项目的根目录下,找到 appsettings.json 文件。在文件中,找到 Kestrel 部分,并修改 Endpoints 属性的值。

例如,要将 HTTP 端口修改为 80,请将 Endpoints 属性的值修改为 http://localhost:80

JSON

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:80"
      }
    }
  }
}

方法三:在代码中修改

在 Program.cs 文件中,找到 builder.WebHost() 方法。在方法中,修改 urls 属性的值。

例如,要将 HTTP 端口修改为 80,请将 urls 属性的值修改为 http://localhost:80

C#

public static void Main(string[] args)
{
  var builder = WebHost.CreateDefaultBuilder(args);

  // 修改 HTTP 端口
  builder.WebHost.Endpoints.DefaultHost.Addresses.Add("http://localhost:80");

  builder.Build().Run();
}

 详细文档:ASP.NET Core 6(.NET 6) 修改默认端口的方法(5000和5001)-CJavaPy 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值