asp.net-mvc – 使用ASP.NET MVC 2 AsyncController实现长时间运
发布时间:2020-12-15 23:40:20 所属栏目:asp.Net 来源:网络整理
导读:在ASP.NET MVC 2中阅读 documentation on AsyncControllers之后,我想知道在这种情况下实现ajax进度条的最佳方法是什么.这个教程似乎有点奇怪, 我猜想实现一个AJAX进度条需要额外的操作方法来返回当前任务的状态.但是,我不知道在工作线程和该操作方法之间交换
在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应用程序?
- 如何找出重新启动ASP.NET Web应用程序的原因
- asp.net-mvc – Windows 8 VS2012 IISExpress Windows身份验
- asp.net – web.config使自定义成员资格提供程序不起作用?
- asp.net-mvc – 验证在部分视图中不起作用
- asp.net – 如何从浏览器历史记录中删除页面?
- asp.net – 自我跟踪实体vs POCO实体
- asp.net-mvc – git和ASP MVC
- asp.net – 如何使用JSON方法序列化javascript对象
- asp.net-mvc – Ninject.Extensions.Logging.nlog2 – 如何
推荐文章
站长推荐
- asp.net-mvc – 如何使WebAPI操作只能从我的应用
- [Hei.Captcha] Asp.Net Core 跨平台验证码实现
- asp.net-mvc-3 – MVC 3 Razor EditorTemplate /
- asp.net-mvc – auth ASP.NET MVC REST API的最佳
- asp.net-mvc – 如何为ASP.NET 4.5 Web API创建M
- asp.net-mvc-3 – System.ComponentModel.DataAn
- ASP.NET MVC5 OWIN:通过Facebook登录后,为什么U
- 经典ASP亚马逊s3休闲授权
- Asp Mvc3 webgrid通过ajax进行分页和过滤
- asp.net-mvc – Request.GetOwinContext在单元测
热点阅读