2

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?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Jack
  • 1
  • 21
  • 118
  • 236

2 Answers2

3

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" : { ... }
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • _“That file is exclusively used by the Visual Studio debugger”_ – That’s not correct. `dotnet run` will also respect the contents of `launchSettings.json` unless you explicitly disable it with `dotnet run --no-launch-profile`. – poke Nov 01 '20 at 18:10
  • @poke I'm pretty sure that's what I read not long ago in a Microsoft Docs doc, do you happen to have a link at hand for that? – Camilo Terevinto Nov 01 '20 at 18:30
  • It’s mentioned [here in the docs](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.1#development-and-launchsettingsjson) under _“Profiles can be selected”_. But you can also simply try it out by deleting the launchSettings.json and seeing that `dotnet run` will no longer launch in development mode :) – poke Nov 01 '20 at 18:45
  • @poke I also read [this](https://dotnettutorials.net/lesson/asp-net-core-launchsettings-json-file/)n and it seems to be true. On the other hand, there is only one port setting as 6000 in `launchSettings.json` and I could use it via CLI. So, what should I do. I am really very confused. – Jack Nov 01 '20 at 19:09
  • @CamiloTerevinto Thanks for your helps. But before your answer, I exactly apply the settimng that you suggest, however it does not work and again starts random ports. How to fix it? Any idea? – Jack Nov 01 '20 at 19:11
  • By the way, voted up for your helps. But I hope I will fix the problem with the help of you :) Any other suggestion? – Jack Nov 01 '20 at 19:12
  • @CamiloTerevinto On the other hand, when running F5 in VS Code, it opens the app with the given port, but the app is not working ("This site can’t be reached" page). – Jack Nov 01 '20 at 19:20
  • 1
    @poke I stand corrected then, thanks! I don't have a dev machine at hand, that's why I didn't try myself :) – Camilo Terevinto Nov 01 '20 at 20:00
  • Yes, it is working on Firefox, but unfortunately does not work on Google Chrome. It is weird, but I also tried by New Incognito Window in Chrome, still not works. Any idea? – Jack Nov 01 '20 at 20:51
  • @Jack It might be caching. Sometimes a browser caches an incorrect success and even an incorrect error. Might want to try using CTRL+F5 to force-refresh Chrome's cache for your site – Camilo Terevinto Nov 01 '20 at 21:21
  • After an inspection, I have seen that there are `https` in one of the urls on this field: `"applicationUrl": "https://localhost:6001;http://localhost:6000"`. So, maybe it is correct, but after removing it works on Chrome also. But I am not sure if one of these address should be https. Any idea? – Jack Nov 01 '20 at 21:29
  • @Jack Chrome might be attempting to connect to HTTPs, but you don't have the IIS Express settings to enable HTTPs – Camilo Terevinto Nov 01 '20 at 21:47
  • Perfect, thanks a lot. I will correct them, it is an important experience for me even if too expensive :) – Jack Nov 01 '20 at 21:55
1

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"
            }
        }
    },

hsd
  • 452
  • 5
  • 12
  • @VoteDowners Why voted down the answer? Yes, it did not work, but it would be better to say why voted down and what is the correct answer? – Jack Nov 01 '20 at 16:58
  • 1
    Because this is completely incorrect, that's why the downvote. You can run from command line IIS express or Kestrel, whichever you want. Also, appsettings.json is completely irrelevant. So, in general, this answer is useless. – Camilo Terevinto Nov 01 '20 at 17:00