39

I am trying to change default port from properties section of project but I am not able to see any options.

I am using visual studio 2022 with .NET core 6.

4
  • Is this an ASP.NET Core API? Or some other kind? Dec 13, 2021 at 10:07
  • Yes, It is .net core api Dec 13, 2021 at 10:10
  • I recommend adding the [asp.net-core] tag then. Dec 13, 2021 at 10:14
  • oops I missed it, thanks Dec 13, 2021 at 10:42

3 Answers 3

85

The port is defined in the endpoints and there are multiple ways to change them:

For Development purposes

You can change in launchSettings.json file inside Properties folder:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:22963",
      "sslPort": 44349
    }
  },
  "profiles": {
    "UrlTest": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7244;http://localhost:5053",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Server Endpoints

There is a file in root folder called appsettings.json with you can change the server related configuration, this is an example with Kestrel:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5400"
      },
      "Https": {
        "Url": "https://localhost:5401"
      }
    }
  }
}

From command line

You can run the application with the --urls parameter to specify the ports:

dotnet run --urls http://localhost:8076

Environment Variable

You can set the ASPNETCORE_URLS.

From source code

You can pass the Url to Run method:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run("http://localhost:6054");

Or the UseUrl extension method:

there was a bug using this method but it seems solved now #38185

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://localhost:3045");
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

Source:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0

A good documentation about the deploy: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/?view=aspnetcore-6.0

2
  • tried "Server Endpoints" method on Ubuntu with Apache and reverse proxy with no success but using "From source code" worked fine
    – Ahmed
    Feb 3, 2022 at 17:44
  • 1
    lanuchSettings.json is broken in dotnet 6.0. See the answer here => stackoverflow.com/questions/69932090/… Sep 15, 2022 at 0:13
7

Quick solution:

In Program.cs :

if (app.Environment.IsDevelopment())
{
    app.Run();
}
else
{
    app.Run("http://127.0.0.1:8080");
}
5

You can set it from the launch profile setting

Click on the Dropdown on the run button.

enter image description here

Now click on debug properties. By clicking on that launch profile window will open.

now you can change the port from the app URL from here.

enter image description here

Edit: Add on

You can also change it from the project profile as below.

enter image description here

1
  • 2
    OP might be using Visual Studio but a lot of developers use Rider or VS Code. A more generic answer would be more helpful.
    – db2
    Sep 12, 2022 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.