Rodrigo Menjivar
Rodrigo Menjivar

Reputation: 3

How to consume web API in the same solution?

I have two projects (.Net Core 3.1) in the same solution, the first one called "BackEnd" is a Web API and the second "FrontEnd" is a web application MVC.

My problem is the Web API Port because it changes everytime I restart my PC or use another Computer and I have to change the port in FrontEnd to keep consuming the API.

Is there a way to get the port of the Web API programmally?

Upvotes: 0

Views: 1305

Answers (2)

Serge
Serge

Reputation: 43870

If both projects are in the same solution you can open "Properties" of the solution and in "Startup Project" tab select "Multiple Start Projects" and select both - "Back End" and "Front End" Projects. And an Api project "Properties", select "Debug" and if needed change the port number next to APP URL. That's it.

Upvotes: 1

Roman Marusyk
Roman Marusyk

Reputation: 24579

Open launchSettings.jsonand specify the port in applicationUrl for configuration that you use

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:10042",
      "sslPort": 44353
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "BackEnd": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:10040;http://localhost:10041"
    }
  }
}

or in Visual Studio, right-click on the project -> Properties, click on Debug and change the port number next to APP URL under the Web Server Settings

enter image description here

Upvotes: 2

Related Questions