从另一个控制器调用异步操作'同步行动

时间:2015-03-17 16:51:52

标签: c# asp.net asp.net-mvc

想象一下,我们在控制器中定义了异步操作:

public class PortalController : AsyncController {
public void NewsAsync(string city) {

    AsyncManager.OutstandingOperations.Increment();
    NewsService newsService = new NewsService();
    newsService.GetHeadlinesCompleted += (sender, e) =>
    {
        AsyncManager.Parameters["headlines"] = e.Value;
        AsyncManager.OutstandingOperations.Decrement();
    };
    newsService.GetHeadlinesAsync(city);
}

public ActionResult NewsCompleted(string[] headlines) {
    return View("News", new ViewStringModel
    {
        NewsHeadlines = headlines
    });
}

当我从浏览器中调用它时 - 一切正常。但我想从另一个同步控制器调用此动作(新闻)。有没有办法调用行动并获得结果?

1 个答案:

答案 0 :(得分:0)

public class PortalController: Controller {
    public ActionResult News(string city) {
        NewsService newsService = new NewsService();
        ViewStringModel headlines = newsService.GetHeadlines(city);
        return View(headlines);
    }
} 

你应该查看code you got From MSDN的其余内容,如果没有某种ActionResult来获取数据,你就无法拥有一个视图。