具有重定向响应的 Angular4+ 和 Asp.NET 核心

Angular4+ and Asp.NET Core with a redirect response

我有一个 ASP.NET 核心 Api,在控制器中有一个 Post 方法和一个调用它的 angular 服务 直到今天,我使用路由器从 angular 中进行导航,但我需要知道它是否可能以及如何从服务器重定向并在将接收的 angular Post 方法上处理这种情况重定向响应而不是某些 JSON 对象。浏览器可以自己处理还是我需要在收到响应后在调用中做一些事情?

客户端和服务器运行在同一台服务器上

控制器

 [HttpPost]
    public ActionResult Post([FromBody]string myvalue)  

    {

    // some operations here

        string parameter = SomeFunctionReturningParameter(myvalue);

            return new RedirectResult("/component2?param=" + parameter);
        }


    }

Angular 服务

public callRedirection(apiEnd:, body: any): Observable<any> {

    return this.http.post(endPoint, body)
      .map((response) => {

    //how to handle redirect here  
        return response; 
      })
     .catch(error => {  console.log(error) });
  }

有什么想法吗???

您应该能够将路由器导入您的组件,并在处理程序中提取路由并导航到 url。

import { Router } from '@angular.router';

...

constructor(private router: Router...) {
...
}

public callRedirection(apiEnd:, body: any): Observable<any> {

return this.http.post(endPoint, body)
  .map((response) => {

    //how to handle redirect here  
    this.router.navigate(response.url)

  })
 .catch(error => {  console.log(error) });

}

注意:我不是 100% 确定如何从响应中获取 URL,但我相信您可以在调试器中解决它:-)