繁体   English   中英

从服务器端到客户端的多个PartialViews异步发送(不确定的延迟)

[英]Sending from server-side to client-side multiple PartialViews asynchronously with an undefined delay

我潜伏了大约一个小时,试图找到解决方案或模式来做(标题参考)。让我举一个例子说明我的意思:

Client-to-Server: Hey, I want information about "ww2" //using jquery's ajax .post() method
Server-to-Client: Ok, got your request, prepare to receive data asynchronously.
Server-to-Client: "World War II happen in 1939 to 1945"
//after couple of seconds
Server-to-Client: "The Alliance won the war."
//some more delay
Server-to-Client: "bla bla bla"
Server-to-Client: "thats it, im done for now".

现在显然,客户端将在使用jquery接收到数据后立即显示它们。 我的主要问题是,如何才能在服务器上的Action上调用HttpPOST并异步返回许多PartialView

如果有任何其他方式/想法与样品,将不胜感激。

使用SignalR是一种选择

但是,如果您想自己做,而不是从Controller继承控制器类,请从AsyncController继承它。 AsyncController继承的控制器可以处理异步请求,并且仍然可以为同步操作方法提供服务。

public class HomeController : AsyncController 
     {
         public void ExtensiveTaskActionAsync() 
         {
             AsyncManager.OutstandingOperations.Increment();
             Task.Factory.StartNew(() => DoExtensiveTask());
         }

         private void DoExtensiveTask()
        {
            Thread.Sleep(5000); // some task that could be extensive
            AsyncManager.Parameters["message"] = "hello world";
            AsyncManager.OutstandingOperations.Decrement();
        }


        public ActionResult ExtensiveTaskActionCompleted(string message) 
        {
            //task complete so, return the result
            return View();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM