我一直潜伏着大约一个小时试图寻找解决方案或模式(标题参考).. 让我举个例子来说明我的意思:
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接收数据后立即显示数据。
我的主要问题是,如何在服务器上的HttpPOST
上调用Action
并异步返回多个PartialView
?
如果样品有任何其他方式/想法,我们将非常感激。
答案 0 :(得分: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();
}
}