【问题标题】:Angular4+ and Asp.NET Core with a redirect response带有重定向响应的 Angular4+ 和 Asp.NET Core
【发布时间】:2018-08-25 19:06:39
【问题描述】:

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

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

控制器

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

    {

    // some operations here

        string parameter = SomeFunctionReturningParameter(myvalue);

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


    }

角度服务

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) });
  }

有什么想法???

【问题讨论】:

    标签: angular http asp.net-core url-redirection angular-services


    【解决方案1】:

    您应该能够将路由器导入组件并在处理程序中提取路由并导航到 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,但我相信你可以在调试器中解决它:-)

    【讨论】:

    • 当我在同一台服务器上运行服务器和客户端时工作,但在分别运行时抛出错误。反正答案很好
    • 你只是从服务器返回 URL 的一部分,所以 Angular 会假设重定向在同一个服务器上。更改您的服务器以返回完整的 URL,例如return new RedirectResult("myhost/component2?param=" + 参数);
    • 该评论在重定向地址的开头标出了 Http :-( 它应该以“Http://”开头,然后是完整的 URL,例如 myHost/Component 等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2018-06-18
    • 2016-09-17
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多