在 MVC 中渲染视图然后立即重定向

发布于 2024-09-15 16:02:54 字数 850 浏览 10 评论 0原文

我想要做的是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归途 2024-09-22 16:02:54

如果不在客户端调用某种 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.

雄赳赳气昂昂 2024-09-22 16:02:54

一个简单的解决方案是关闭后台进程,然后显示一个页面,其中 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文