Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default port for ASP.NET Core in VS Code?

I develop an Angular app based on ASP.NET Core and there is some settings in launchSettings.json in order to run the app with the given ports as shown below:

"profiles": {
  "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
      "DOTNET_ENVIRONMENT": "Development"
    }
  },
  "EmployeeProject.WebUI": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:6000;https://localhost:6001",
      "environmentVariables": {
      "DOTNET_ENVIRONMENT": "Development"
    }
  }
}

However, I heard that these settings is ignored when using VS Code and when running the frontend and backend at the same time using dotnet watch run the app starts by using random ports in every run. So, how can I make the app starts using the same ports (6000 or 6001) via dotnet run or dotnet watch run in VS Code?

like image 514
Jack Avatar asked Oct 29 '25 09:10

Jack


2 Answers

If you want to do this the VS Code way, as long as you use F5 (or the Run > "Start Debugging" command), it's as simple as changing the launch.json file from this:

...
"env": {
    "ASPNETCORE_ENVIRONMENT": "Development"
},
...

to this:

...
"env": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_URLS": "http://localhost:5001"
},
...

Otherwise, if you use the IIS Express profile, edit your launchSettings.json file to specify the port:

"iisSettings": {
  "iisExpress": {
    "applicationUrl": "http://localhost:6000",
  }
},
"profiles" : { ... }
like image 76
Camilo Terevinto Avatar answered Oct 31 '25 22:10

Camilo Terevinto


Running from command line execute Kastrel server not IIS. In that case probably configuration appsettings.json is use. You can put in this configuration section to control port:

    "Kestrel": {
        "Endpoints": {
            "HTTP": {
                "Url": "http://localhost:6000"
            }
        }
    },

like image 33
hsd Avatar answered Oct 31 '25 23:10

hsd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!