0

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

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 接收到数据后立即显示数据。我的主要问题是,我怎样才能在服务器上调用 aHttpPOST并异步Action返回许多s?PartialView

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

4

1 回答 1

1

使用 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();
        }
    }
于 2013-07-01T22:20:35.017 回答