Question: All attempts to expose a .NET 6 web API on port 8888 fail.

I tried [these answers](https://stackoverflow.com/questions/37365277/how-to-specify-the-port-an-asp-net-core-application-is-hosted-on) and various other articles online, by adding urls in appsettings.json and configuring the port in Program.cs without success.

**Update:**
The web API starts on the default port and not on the port I want to expose it to. When configuring ports in Program.cs, I receive

> 
> System.InvalidOperationException: 'Changing the URL is not supported
> because Addresses IsReadOnly.'
> 
> 
> 

```
app.Run("http://localhost:8888"); // This code results in Exception above

```

or

> 
> System.NotSupportedException: 'Collection was of a fixed size.'
> 
> 
> 

```
app.Urls.Add("http://localhost:8888"); // The Exception above occurs after running this line of code

```

Running the code below doesn't change the port, and the API starts on the default port

```
builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(8888));

```

Answer: 
Adding the line `builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(PortNumber));`
to your Program.cs file should allow you to change the default port on a .NET 6 web API.

```
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(8888));

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

```

The above change should work also when you are debugging, but another way to change this in development as mentioned in the other answer, you can use the launchSettings.json file under the properties folder of the solution, documentation related to this file and how it works can be found [here](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-6.0), an example of what you want would be:

```
{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:31312",
      "sslPort": 44346
    }
  },
  "profiles": {
    "ProfileNameHere": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:8887;http://localhost:8888",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

```

The profile section will affect kestrel ports in dev environment so that is the one you should be updating to the desired value. You can also create multiple profiles with different ports and configurations.
Can you try this

```
 public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }

            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)                 
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>).UseKestrel().UseUrls("http://0.0.0.0:8888");
                    });
        }

```

This works for docker environment also. Make the port as variable and get it from environment as next step