Kafkalasch
Kafkalasch

Reputation: 65

How to make Visual Studio starting my ASP.NET application always on the same port when debugging?

I am developing an ASP.NET Core Web API in Visual Studio and using Postman to test it.

To do so, I start the integrated IIS Express Server via clicking on the "Run" Button in Visual Studio. I would expect the server always listening on the same port but on every start it is assigned a different port. How can I change this?

This is my output of the ASP.NET Core Web Server in Visual Studio after I started and stopped the server 3 times:

WebApi> Hosting environment: Development
WebApi> Content root path: C:\Code\Work\webcoreapi\WebApi
WebApi> Now listening on: http://localhost:25626
WebApi> Application started. Press Ctrl+C to shut down.
WebApi> Hosting environment: Development
WebApi> Content root path: C:\Code\Work\webcoreapi\WebApi
WebApi> Now listening on: http://localhost:22130
WebApi> Application started. Press Ctrl+C to shut down.
WebApi> Hosting environment: Development
WebApi> Content root path: C:\Code\Work\webcoreapi\WebApi
WebApi> Now listening on: http://localhost:22405
WebApi> Application started. Press Ctrl+C to shut down.

If I run the same Application in command line via dotnet run it starts the application and listens on localhost:5000 as expected every single time.

So it has to do something with my Visual Studio Configuration but I cannot find it. Here is my launchSettings.jsonfile:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:5000/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchUrl": "http://localhost:5000/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

So I guess everything is correctly configurated. I know that Visual Studio tries to find an open port if the default one is not availability but I already checked that too. The port 5000 is always free to use when I start the server in VS.

Does anyone have a hint how to make VS always start on 5000?

I am using Visual Studio Community 2017 (Version 15.3.5)

Upvotes: 3

Views: 1893

Answers (1)

RoninEngineer
RoninEngineer

Reputation: 396

Under properties in the startup project, look for launchSettings.json.

You can set the applicationURL value there. I.e. http://localhost:5000 The environment variables and launch URL can be set there as well.

Upvotes: 1

Related Questions