asp.net-mvc – 使用ASP.NET MVC 2 AsyncController实现长时间运行的任务的进度条
|
在ASP.NET MVC 2中阅读 documentation on AsyncControllers之后,我想知道在这种情况下实现ajax进度条的最佳方法是什么.这个教程似乎有点奇怪, 我猜想实现一个AJAX进度条需要额外的操作方法来返回当前任务的状态.但是,我不知道在工作线程和该操作方法之间交换有关任务状态的信息的最佳方式. 到目前为止,我最好的想法是将信息进入会话字典和唯一的ID,并与客户端共享该id,以便它可以轮询状态.但也许有一个更简单的方法,我没有注意到. 最好的方式是做什么? 谢谢, 阿德里安 解决方法非常有趣的问题!实际上它似乎不是AsyncController的任务.异步控制器设计用于在服务器端长时间运行的单HTTP查询操作.当您使用异步操作时,这只能帮助您在某些长时间运行的操作期间释放ASP.Net工作线程,并允许在执行操作时提供其他请求.但从客户端来看,这并不重要,这是不是同步控制器吗?对于客户端,这只是单个HTTP请求.您需要在应用程序中使用一些长时间运行的查询服务来重新设计.以下是控制器的示例,可以为此工作流提供服务: public class LongOperationsController : Controller
{
public ActionResult StartOperation(OperationData data)
{
Guid operationId = Guid.NewGuid(); // unique identifier for your operation
OperationsService.DoStartOperation(operationId,data); // service starts to perform operation using separate thread
return new JsonResult(operationId); // operation id should be sent to client to allow progress monitoring
}
public ActionResult GetOperationStatus(Guid operationId)
{
var status = OperationsService.GetStatus(operationId); // this method returns some object,that describes status of operation (e.g. progress,current task etc.)
return new JsonResult(status); // returning it to client
}
public ActionResult GetOperationResult(Guid operationId)
{
var result = OperationsService.GetOperationResult(operationId); // this should throw exception if operation is not yet completed
return new JsonResult(result);
}
public ActionResult ClearOperation(Guid operationId)
{
OperationsService.ClearOperationResult(operationId); // we should delete operation result if it was handled by client
return true;
}
}
这里是客户端代码,可以与此控制器进行交互: var operationId;
function startOperation(data) {
$.post('/LongOperations/StartOperation',data,function(response) {
operationId = response; // store operationId
startOperationMonitoring(); // start
},'json');
}
function startOperationMonitoring() {
// todo : periodically call updateOperationStatus() to check status at server-side
}
function updateOperationStatus() {
// todo : get result of GetOperationStatus action from controller
// todo : if status is 'running',update progress bar with value from server,if 'completed' - stop operation monitoring and call finishOperation()
}
function finishOperation() {
// todo : get result of GetOperationResult action from controller and update UI
// todo : call ClearOperation action from controller to free resources
}
这是非常基本的概念,这里有一些错过的项目,但我希望你会得到主要的想法.还有,您如何设计该系统的组件,例如: >为OperationsService使用单例, 祝你好运! (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – redirectToAction()和View()之间的区别
- asp.net-web-api – 具有Web Api RC的Ninject InSingletonS
- asp.net – 如何添加一个访问控制允许原始头在IIS7有限制
- asp.net – 如何检查文档是否准备好?
- .net – 你用你的单元测试测试什么?
- asp.net-mvc-4 – 如何在MVC 4 w / default simplemembersh
- asp.net-mvc-4 – Resharper无法解析主页中的部分视图
- asp.net – 如何在构建期间从Visual Studio调用aspnet_comp
- asp.net-mvc – 用于创建具有模型和ViewData项目的ViewData
- asp.net-web-api – SignalRWebApi – 碰撞Newtonsoft.Json
- asp.net-mvc – Razor视图引擎.cshtml页面中的Ou
- asp.net-mvc – WebService还是一个简单的MVC控制
- asp.net-mvc – ASP.Net MVC加载进度指标
- asp.net-mvc – 使用ASP.NET MVC /实体框架对POC
- asp.net-mvc-4 – 如何在服务器上安装ASP.NET MV
- asp.net-mvc – 在控制器外部生成路由,类似于Url
- asp.net-core-mvc – 如何从任何ASP .Net Core类
- asp.net – 如果我没有指定targetFramework =“4
- asp.net页面中的静态变量
- asp.net – 如何MSDeploy构建的网站包到一个处女
