如何在MVC中执行当前操作时重定向到其他页面 [英] How redirect to other page while current action is getting executed in MVC

查看:114
本文介绍了如何在MVC中执行当前操作时重定向到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Mvc应用程序中有两个菜单项,我想允许用户在执行当前(第一个)操作时单击时导航第二个菜单。第一个操作是异步操作但仍然阻止用户直到它完成



我尝试了什么:



我阻止用户不进入当前正在执行的其他操作,但我的要求是我应该允许用户进入其他页面,而当前正在执行

I Have Two Menu Items in My Mvc Application, I want to Allow user to navigate second menu when he clicks while the current(first) action is getting executed, First action is asynchronous action but still its blocking user until it gets completed

What I have tried:

I blocked user not go into other action while current is getting executed but my requirement is I should allow the user to go into other pages while current one being executing

推荐答案

这不是HTTP的工作方式。 HTTP适用于请求和响应。虽然您可以远离原始URL,但它所做的任何请求都可能被取消。



我建议的最好的事情就是在服务器上安装URL1一个包装器,它定义一个任务然后运行它。一旦Task.Run()启动,你就可以返回正确的响应(应该是200:接受)。

这应该几乎立即满足你的AJAX请求,以便第二个链接可以被作用



参考:

c# - 在异步任务完成之前返回 - 堆栈溢出 [ ^ ]
That's not quite how HTTP works. HTTP works on the Request AND Response. While you can navigate away from the original URL, any requests it made are likely to be canceled.

The best thing I could recommend is to have URL1 on the server just a "wrapper" which defines a task and then runs it. Once Task.Run() starts you can then return the proper response (should be 200: Accepted).
This should satisfy your AJAX request almost immediately so that the second link can be acted upon

Reference:
c# - Return before async Task complete - Stack Overflow[^]


这是您的问题的重新发布,在MVC操作中显示异步过程的可视结果 [ ^ ]。评论中的内容有助于理解问题。



问题是你的任务是异步的,这意味着没有关于需要花费多少时间的信息。因此,最好使用后台服务来跟踪该任务,并在布局中显示进度。这样,您将解耦当前页面和任务。



您可以利用后台线程来执行这些操作。线程池线程是有限的,您将很快用完线程。创建新线程既昂贵又昂贵,并且可能根本不是一个好的解决方案。这样,如果我不得不这样做,我会考虑使用某种事件处理,比如消息队列来管理和维护某些事件和操作的状态。每个操作都可以有其状态,进度,我可以使用它来更新用户。



查看这些链接以获得实用的概述,

如何在ASP.NET中运行后台任务 - Scott Hanselman [ ^ ]

c# - 如何在asp.net下异步运行持久的进程? - 堆栈溢出 [ ^ ]

带进度对话框的简单异步后台任务处理(Silverlight解决方案) [ ^ ]
This is a repost of your question, Display visual result of async process in MVC action[^]. The content from your comment helps in understanding the question.

The problem is that your tasks are asynchronous, meaning there is no information as to how much time it will take. Thus, it is a good practice to use a background service to keep a track of that task and you show the progress in the layout. This way, you will decouple the current page and the task.

You can utilize background threads to perform these actions. Thread pool threads are limited, and you will run out of threads quickly. Creating new threads is expensive and costly, and might not be a good solution at all. In this way, if I had to do this, I would consider using some sort of event-processing, like a message queue to manage and maintain the state of certain events and operations. Each operation can have its state, progress, and I can use that to update the users.

Check these links for a practical overview,
How to run Background Tasks in ASP.NET - Scott Hanselman[^]
c# - How to run long-lasting process asynchronously under asp.net? - Stack Overflow[^]
Simple Asynchronous Background Task Processing with Progress Dialog (Silverlight Solution)[^]


这篇关于如何在MVC中执行当前操作时重定向到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆