如何更改 asp.net Core 3 或 Net Core 5 中的默认端口

How to change the default port in asp.net Core 3 or Net Core 5

当我在调试时,为了更改默认端口,我修改了 launchSettings.json 文件,并更改了端口

"WebApplication1": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://*:8081;http://*:8080"
    }

但是如果我在文件夹 (selfHost) 中发布应用程序并启动可执行文件,它总是在端口 5000 上侦听 有人知道如何更改生产中的默认端口。 我尝试在 program.cs 中使用 UseUrls 更改它,但没有工作

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                }).UseWindowsService()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>()
                    .UseUrls("http://*:8080","https://*:8081");
                })
            .UseSerilog();

您可以通过更改 LaunchSettings.json.

来简单地更改端口

可以通过属性->LaunchSettings.json找到。

{
  "iisSettings": {
  "iisExpress": {
  "applicationUrl": "http://localhost:8080",
  "sslPort": 96085<== Change_This as you wish
  }
},

终于明白了
之前

   webBuilder.UseStartup<Startup>();

添加

 webBuilder.UseUrls("https://*:8081", "http://*:8080");

这是代码

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                }).UseWindowsService()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("https://*:8081", "http://*:8080");
                    webBuilder.UseStartup<Startup>();
                })
            .UseSerilog();
}

我希望它对其他人有用。 谢谢

使用命令行参数

dotnet 运行 --urls "http://localhost:5100;https://localhost:5101"

  • 或-

dotnet /Product/Full/Path/Product.dll --urls "http://localhost:5100;https://localhost:5101"