C# 从另一个控制器操作中调用AsyncController操作?
我想完成以下几点:C# 从另一个控制器操作中调用AsyncController操作?,c#,asp.net-mvc,.net-3.5,asynchronous,C#,Asp.net Mvc,.net 3.5,Asynchronous,我想完成以下几点: class SearchController : AsyncController { public ActionResult Index(string query) { if(!isCached(query)) { // here I want to asynchronously invoke the Search action } else {
class SearchController : AsyncController
{
public ActionResult Index(string query)
{
if(!isCached(query))
{
// here I want to asynchronously invoke the Search action
}
else
{
ViewData["results"] = Cache.Get("results");
}
return View();
}
public void SearchAsync()
{
// some work
Cache.Add("results", result);
}
}
我计划从客户端进行AJAX“ping”,以便知道结果何时可用,然后显示它们
但我不知道如何以异步方式调用异步操作
多谢各位。
Luis您可以在新线程中调用该操作:
if(!isCached(query))
{
new Thread(SearchAsync).Start();
}
视图可以使用AJAX调用检查结果是否可用的操作:
public ActionResult Done(string query)
{
return Json(new
{
isDone = !isCached(query),
result = Cache.Get(query)
});
}
而平:
var intId = setInterval(function() {
$.getJSON('/search/done', { query: 'some query' }, function(json) {
if (json.isDone) {
clearInterval(intId);
// TODO : exploit json.result
} else {
// TODO: tell the user to wait :-)
}
});
}, 2000);