Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 351
ja73 Avatar asked Nov 18 '19 18:11

ja73


People also ask

How do I change the port in .NET core?

To change the port the application is using, Open the file lunchSetting. json. You will find it under the properties folder in your project and as shown below. Inside the file, change applicationUrl port (below is set to 5000) to a different port number and save the file.

How can I change my port number in VS 2019?

Here is a fast recipe on how to manually change the port number to whatever number you want: In Solution Explorer, right-click the name of the web application and select Properties. Click the Web tab. In the Servers section, under dropdown selection for IIS Express, change the port number in the Project URL box.

How do I change my port number for VS 2017?

In VS 2017, I was able to update it in the project properties, instead of the solution properties. Right-click on your project file, then choose Properties. Simply modify it to your desired port number and save.


2 Answers

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

like image 127
ja73 Avatar answered Sep 22 '22 00:09

ja73


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
  }
},
like image 26
MK Vimalan Avatar answered Sep 20 '22 00:09

MK Vimalan