在 MVC 中渲染视图然后立即重定向
我想要做的是在 MVC 站点中渲染一个视图,通知用户在服务器端处理发生时不要刷新浏览器。这可能是一个长时间运行的任务,如果他们点击刷新,它将再次发送请求,从而在该过程实际成功时将他们发送到错误屏幕。我打算在 JS 中使用 JQuery $.ajax(...) 或简单的 $(document).ready(function() { window.location = ... }); 来完成此操作。但我希望有一种方法可以在 MVC 中做到这一点,从而使我能够更好地控制返回给客户端调用的 HttpResponseCode。有办法做到这一点吗?
我在想,
public ActionResult LoadingAsync(string UserKey, string userInf)
{
AsyncManager.OutstandingOperations.Increment();
CallProcess(...)
return View();
}
然后让一个完成的操作执行重定向
public ActionResult LoadingCompleted()
{
LongRunningProcess();
return Redirect("http://yourdone.com");
}
,或者只是在视图内渲染一些东西,从视图内部执行重定向有
<% Html.RenderAction("Process"); %><!--This won't actually redirect-->
什么想法吗?
What I am trying to do is render a View in an MVC site informing the user to not refresh their browser while server side processing is taking place. This could be a long running task, and if they hit refresh it will send the request again, thus sending them to the error screen when the process was actually successful. I was going to do this in JS, either with JQuery $.ajax(...) or with a simple $(document).ready(function() { window.location = ... }); but I was hoping there was a way to do it in MVC, thus giving me more control over the HttpResponseCode which is returned to the client calling. Is there a way to do this?
I was thinking along the lines of
public ActionResult LoadingAsync(string UserKey, string userInf)
{
AsyncManager.OutstandingOperations.Increment();
CallProcess(...)
return View();
}
then have a Completed Action perform the redirect
public ActionResult LoadingCompleted()
{
LongRunningProcess();
return Redirect("http://yourdone.com");
}
or just have something that Renders inside the view that will perform the Redirect from inside the View
<% Html.RenderAction("Process"); %><!--This won't actually redirect-->
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不在客户端调用某种 Javascript,就无法实现此目的。
当您向用户返回响应时,您可以返回一个带有 HTTP 代码 200(正常)的页面来显示,或者返回一个带有 HTTP 代码 301(永久移动)或 307(临时移动)和 URL 的重定向指令重定向到。
您必须选择这些返回值中的一个,并且不能同时返回两者。
最简单的解决方案是使用 Javascript 在确定后台进程已完成后将用户从“请稍候”页面重定向到目的地。
There is no way to achieve this without invoking some kind of Javascript on the client.
When you return a response to the user, you either return a page to display with an HTTP code of 200 (OK), or an instruction to redirect with an HTTP code of 301 (Moved Permanently) or 307 (Moved Temporarily) and a URL to redirect to.
You have to choose either of these return values and cannot return both.
The simplest solution is to use Javascript to redirect the user from your "please wait" page to the destination once it determines the background process has completed.
一个简单的解决方案是关闭后台进程,然后显示一个页面,其中 (1) 要求用户不要刷新,(2) 通过 Javascript 每隔几秒轮询一次服务器以确定后台进程是否完成,以及( 3) 确定完成后进行重定向。
One simple solution would be to fire off your background process, then display a page which (1) asks the user not to refresh, (2) polls the server every couple of seconds via Javascript to determine if the background process is complete, and (3) redirects upon determining that it is complete.