ja73
ja73

Reputation: 533

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

when I am in debug, to change the default port, I modify the launchSettings.json file, and change the port

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

but if I publish the application in a folder (selfHost) and launch the executable, it always listens on port 5000 Someone knows how to change the default port in production. I tried changing it in the program.cs with UseUrls but not working

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();

Upvotes: 15

Views: 23350

Answers (3)

Stephen LAI
Stephen LAI

Reputation: 220

Use command line arguments

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

  • OR -

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

Upvotes: 10

ja73
ja73

Reputation: 533

I finally got it
before

   webBuilder.UseStartup<Startup>();

add

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

this is the code

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();
}

I hope it can be useful to someone else. thank you

Upvotes: 26

MK Vimalan
MK Vimalan

Reputation: 1288

Yoy can simple changing the port via changing the LaunchSettings.json.

you can find by Properties-> LaunchSettings.json.

enter image description here

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

Upvotes: 7

Related Questions