How to use angular2's http with other port than application host port?

I have a ASP.NET solution with both a web project that hosts my angular2 app and a web api project.

Both projects are setup as startup projects and are running on different ports (45365 for the web project and 20234 for the web api project).

Let´s say I have a web api controller that exposes /api/values which is currently accessible via http://localhost:20234/api/values

How can I access this in my angular2 web app? If I try


it tries to access http://localhost:45365/api/values which is not desired. However, I want it to call http://localhost:20234/api/values without specifying the whole url including domain to make my service work even when the app is published to a public server with an other domain than localhost.

How do I tell http.get()to use 20234 as port number?

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of
constructor(private location:Location, private locationStrategy:LocationStrategy) {}

someMethod() {
  var base = this.locationStrategy.getBaseHref();
  // replace port

  this.location.prepareExternalUrl(base + 'xxx');

You can implement this in a service that forwards to Http so you don’t need to repeat it.

(not tested)