How to change default port no of my .NET core 6 API

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)