当在使用 asp.net core 与 react 开发时,可能会遇到重定向问题,这时可以通过使用 BFF(Backend for Frontend)模式来解决。BFF 模式是指在前端与后端之间增加一个中间层,将前端与后端分离,实现更好的扩展性和灵活性。具体实现如下:
[HttpGet("my-api")]
public IActionResult GetMyApi(){
return Redirect("/my-react-page");
}
fetch('/my-api', {
method: 'GET',
})
.then(response => {
if(response.redirected){
window.location.href = response.url;
}
});
[HttpGet("my-bff")]
public IActionResult GetMyBff(){
return Redirect("/my-react-page");
}
// 前端使用 fetch 请求
fetch('/my-bff', {
method: 'GET',
})
.then(response => {
if(response.redirected){
window.location.href = response.url;
}
});
通过上述 BFF 模式,可以有效的解决 asp.net core with react 重定向问题。