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.
There are many solutions had been noted in this page, you can check <https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/>

For me,

* When I use an IDE, I modify the file `Properties\launchSettings.json`
* When I run the app in console, I add the param `--urls "http://localhost:5100"`
* When I deploy it on IIS, well, it's not a question because IIS always ask me to enter the port. There is no default port

Note: If they don't work on your side, check there any mistake. Maybe you can restart your PC to make sure the port are available.

Update:

I have tried to run at port 8888 and everything is working well (I also use .NET 6) [![enter image description here](https://i.stack.imgur.com/iEHfO.png)](https://i.stack.imgur.com/iEHfO.png)

[![enter image description here](https://i.stack.imgur.com/hK8jO.png)](https://i.stack.imgur.com/hK8jO.png)