14

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();
ja73
  • 513
  • 2
  • 6
  • 11
  • these all say localhost. did you also use the domain name of production when you put it in production? – Hogan Nov 18 '19 at 18:48
  • the application is selfhosted, that example is in debug – ja73 Nov 18 '19 at 18:50
  • 2
    Possible duplicate of [How to change the port number for Asp.Net core app?](https://stackoverflow.com/questions/38755516/how-to-change-the-port-number-for-asp-net-core-app) – Pavel Kovalev Nov 18 '19 at 18:50
  • the domain name does not have to do if it is self hosted. If it is on a server then it can't be on localhost. localhost is only for local machines. check the question you are a duplicate of ... it is explained well there. – Hogan Nov 18 '19 at 18:51

3 Answers3

25

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

ja73
  • 513
  • 2
  • 6
  • 11
  • 3
    FYI, you can also set an environment variable, so that the values aren't hardcoded into your app. `ASPNETCORE_URLS=https://*:8081;http://*:8080` should do it. – Kirk Larkin Nov 18 '19 at 22:03
10

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"

Stephen LAI
  • 220
  • 3
  • 5
7

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
  }
},
MK Vimalan
  • 1,213
  • 14
  • 28