I have a ASP.NET Core Api with a Post method in a Controller and an angular service that makes a call to it Till today I make navigation from within angular with the router, but I need to know if its possible and how to redirect from the server and handle this situation on angular Post method that will receive a redirect response and not some JSON object. Can the browser handle this by itself or I need to do something within the call after receiving the response?
client and server running on the same server
Controller
[HttpPost]
public ActionResult Post([FromBody]string myvalue)
{
// some operations here
string parameter = SomeFunctionReturningParameter(myvalue);
return new RedirectResult("/component2?param=" + parameter);
}
}
Angular service
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) });
}
Any Idea???
You should be able to import the Router into your component and in the handler extract the route and navigate to the 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) });
}
NOTE: I'm not 100% sure how to get the URL from the response, but I'm sure you can work it out in the debugger :-)