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

3

15

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();
Severalty answered 18/11, 2019 at 18:46 Comment(4)
these all say localhost. did you also use the domain name of production when you put it in production?Phlyctena
the application is selfhosted, that example is in debugSeveralty
Possible duplicate of How to change the port number for Asp.Net core app?Erine
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.Phlyctena
S
26

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

Severalty answered 18/11, 2019 at 19:45 Comment(1)
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.Jollenta
C
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"

Chromato answered 13/8, 2020 at 7:4 Comment(0)
C
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
  }
},
Chamness answered 18/11, 2019 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.