使用服务器传输ASP.Net MVC将AJAX请求重定向到另一个页面/ View

时间:2022-10-07 16:50:30

Is there a way that a MVC Action, initiated by an AJAX request, can redirect the response directly to another page/View without sending a JavaScript “window.location=…” to the client first? In other words, directly transferring the response on the server side and avoiding the roundtrip to the client.

有没有办法让一个由AJAX请求启动的MVC Action可以直接将响应重定向到另一个页面/ View而不首先向客户端发送JavaScript“window.location = ...”?换句话说,直接在服务器端传输响应并避免往返于客户端。

This is a more general question about the possibility to transfer directly an AJAX call, but here is a more specific scenario:

这是关于直接传输AJAX调用的可能性的一般性问题,但这是一个更具体的场景:

  1. The browser sends an AJAX request to the server and based on the request data the controller's logic performs some operations.
  2. 浏览器向服务器发送AJAX请求,并根据请求数据控制器的逻辑执行某些操作。

  3. For the majority of the cases, the controller needs to return back a result (JSON) to the same page. However, for few cases it needs to redirect to another page and it returns back a script to redirect the page, but this causes another roundtrip to the browser. The flow is: Page – Server – Page – Server – New Page, and the question is if this can be optimized to Page – Server – New Page .
  4. 对于大多数情况,控制器需要将结果(JSON)返回到同一页面。但是,在少数情况下,它需要重定向到另一个页面并返回一个脚本以重定向页面,但这会导致另一个往返浏览器的往返。流程是:页面 - 服务器 - 页面 - 服务器 - 新页面,问题是这是否可以优化到页面 - 服务器 - 新页面。

1 个解决方案

#1


0  

This isn't really possible, no.

这不可能,不。

The browser has two options to choose between:

浏览器有两个选项可供选择:

  • Go to a page
  • 转到页面

  • Make an AJAX call to a page
  • 对页面进行AJAX调用

It can't make an AJAX call and automatically redirect based on what the server decides because the client (browser) doesn't know what the server has decided on before the response is even received by the browser.

它无法进行AJAX调用并根据服务器决定的内容自动重定向,因为客户端(浏览器)在浏览器甚至收到响应之前不知道服务器已决定了什么。

The traditional approach (which you're avoiding) is:

传统的方法(你要避免)是:

  • The browser makes an asynchronous request to the server
  • 浏览器向服务器发出异步请求

  • The server responds with data informing the client to redirect
  • 服务器响应数据通知客户端重定向

  • The client makes a new request to the server via window.location
  • 客户端通过window.location向服务器发出新请求

  • The server responds with the new page
  • 服务器响应新页面

However you want to skip a step by doing:

但是,您想要跳过以下步骤:

  • The browser makes an asynchronous request to the server
  • 浏览器向服务器发出异步请求

  • The client makes a new request to the server via window.location
  • 客户端通过window.location向服务器发出新请求

  • The server responds with the new page
  • 服务器响应新页面

But the client doesn't know that it needs to redirect and/or it doesn't know how to.

但客户端不知道它需要重定向和/或它不知道如何。

One (ugly) possibility

One idea that comes to mind to pseudo-achieve what you want is to have MVC render the required view and send the whole thing back to the client via JSON rather than a redirection approach. Let me explain.

想到伪实现你想要的一个想法是让MVC呈现所需的视图并通过JSON而不是重定向方法将整个事物发送回客户端。让我解释。

  • The browser makes an asynchronous request to the server
  • 浏览器向服务器发出异步请求

  • The server determines which page needs to be displayed and renders the view (Generating a view without a controller in MVC)
  • 服务器确定需要显示哪个页面并呈现视图(在MVC中生成没有控制器的视图)

  • The server serialises the resulting HTML as JSON
  • 服务器将生成的HTML序列化为JSON

  • The client receives the JSON, and re-renders the entire page with the new markup (i.e. completely replace)
  • 客户端接收JSON,并使用新标记重新呈现整个页面(即完全替换)

You can also alter the URL and history of the browser to make it "feel" like a redirect by manipulating the browser history.

您还可以通过操纵浏览器历史记录来更改浏览器的URL和历史记录,使其“感觉”像重定向一样。

However I would like to point out that this "solution" is more of an amusing/interesting approach. This isn't really a good way to go about the problem.

但是,我想指出,这种“解决方案”更多是一种有趣/有趣的方法。这不是解决问题的好方法。

#1


0  

This isn't really possible, no.

这不可能,不。

The browser has two options to choose between:

浏览器有两个选项可供选择:

  • Go to a page
  • 转到页面

  • Make an AJAX call to a page
  • 对页面进行AJAX调用

It can't make an AJAX call and automatically redirect based on what the server decides because the client (browser) doesn't know what the server has decided on before the response is even received by the browser.

它无法进行AJAX调用并根据服务器决定的内容自动重定向,因为客户端(浏览器)在浏览器甚至收到响应之前不知道服务器已决定了什么。

The traditional approach (which you're avoiding) is:

传统的方法(你要避免)是:

  • The browser makes an asynchronous request to the server
  • 浏览器向服务器发出异步请求

  • The server responds with data informing the client to redirect
  • 服务器响应数据通知客户端重定向

  • The client makes a new request to the server via window.location
  • 客户端通过window.location向服务器发出新请求

  • The server responds with the new page
  • 服务器响应新页面

However you want to skip a step by doing:

但是,您想要跳过以下步骤:

  • The browser makes an asynchronous request to the server
  • 浏览器向服务器发出异步请求

  • The client makes a new request to the server via window.location
  • 客户端通过window.location向服务器发出新请求

  • The server responds with the new page
  • 服务器响应新页面

But the client doesn't know that it needs to redirect and/or it doesn't know how to.

但客户端不知道它需要重定向和/或它不知道如何。

One (ugly) possibility

One idea that comes to mind to pseudo-achieve what you want is to have MVC render the required view and send the whole thing back to the client via JSON rather than a redirection approach. Let me explain.

想到伪实现你想要的一个想法是让MVC呈现所需的视图并通过JSON而不是重定向方法将整个事物发送回客户端。让我解释。

  • The browser makes an asynchronous request to the server
  • 浏览器向服务器发出异步请求

  • The server determines which page needs to be displayed and renders the view (Generating a view without a controller in MVC)
  • 服务器确定需要显示哪个页面并呈现视图(在MVC中生成没有控制器的视图)

  • The server serialises the resulting HTML as JSON
  • 服务器将生成的HTML序列化为JSON

  • The client receives the JSON, and re-renders the entire page with the new markup (i.e. completely replace)
  • 客户端接收JSON,并使用新标记重新呈现整个页面(即完全替换)

You can also alter the URL and history of the browser to make it "feel" like a redirect by manipulating the browser history.

您还可以通过操纵浏览器历史记录来更改浏览器的URL和历史记录,使其“感觉”像重定向一样。

However I would like to point out that this "solution" is more of an amusing/interesting approach. This isn't really a good way to go about the problem.

但是,我想指出,这种“解决方案”更多是一种有趣/有趣的方法。这不是解决问题的好方法。