asp.net - 如何将 angular2 的 http 与应用程序主机端口以外的其他端口一起使用?

标签 asp.net http angular

我有一个 ASP.NET 解决方案,其中包含一个托管我的 angular2 应用程序的 Web 项目和一个 Web API 项目。

这两个项目都设置为启动项目并在不同的端口上运行(45365 用于 web 项目,20234 用于 web api 项目)。

假设我有一个公开 /api/values 的 web api Controller ,目前可通过 http://localhost:20234/api/values 访问/p>

如何在我的 angular2 网络应用程序中访问它?如果我尝试

this.http.get("api/values")

它尝试访问不需要的 http://localhost:45365/api/values。但是,我希望它调用 http://localhost:20234/api/values 而不指定包括域在内的整个 url 以使我的服务工作,即使应用程序与其他服务器一起发布到公共(public)服务器域比 localhost

如何告诉 http.get() 使用 20234 作为端口号?

最佳答案

constructor(private location:Location, private locationStrategy:LocationStrategy) {}

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

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

您可以在转发到 Http 的服务中实现这一点,因此您无需重复。

(未测试)

关于asp.net - 如何将 angular2 的 http 与应用程序主机端口以外的其他端口一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36625179/

相关文章:

asp.net - 重建后 ASP.NET/IIS 中的 session 为空

c# - 使用多个asp创建Bootstrap-Datepicker :Repeater TextBoxes

javascript - 使用 css 和 javascript 突出显示单击文本框 div

java - 多次调用 java.util.zip.Deflater.setInput 在第一次之后什么都不做

javascript - 避免 Angular 中的多个服务实例

Angular 6查找并删除对象

ASP.NET MVC 在安全内容和非安全内容之间切换

c++ - 在 C 中使用原始套接字确定 HTTP 版本

http - iframe 中的字符编码

javascript - Angular 2/4 : Assign array value from promise function in service layer to a variable in component