Tassisto
Tassisto

Reputation: 10345

How do I expose Web API on different port than default? .NET 6

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

I tried these answers 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));

Upvotes: 1

Views: 10927

Answers (3)

Armando Bracho
Armando Bracho

Reputation: 729

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, 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.

Upvotes: 8

Võ Quang Hòa
Võ Quang Hòa

Reputation: 3025

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

enter image description here

Upvotes: 1

Santosh Karanam
Santosh Karanam

Reputation: 1217

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

Upvotes: 0

Related Questions