开发者

how i can make asynchronus result in ASP.NET MVC 3?

开发者 https://www.devze.com 2023-02-24 22:46 出处:网络
i want a thing in JsonResult that they respond after the got request from the browser [client side] and respond them quickly before done the task.

i want a thing in JsonResult that they respond after the got request from the browser [client side] and respond them quickly before done the task.

means request come respond before task done and run a thread to done the task.开发者_StackOverflow社区

can anyone show me the code for doing that in asp.net MVC


Isn't AJAX sufficient for your scenario?

$.getJSON('@Url.Action("Foo")', function(result) {
    // once the task completes this callback will be executed
});
// flow continues to execute normally

and on the server side:

public ActionResult Foo()
{
    // TODO: some task
    return Json(someResult, JsonRequestBehavior.AllowGet);
}

If this task is I/O intensive you could take advantage of asynchronous controllers and I/O Completion Ports.

If you have a fire-and-forget scenario you could simply start the task and return immediately from the controller:

public ActionResult StartTask()
{
    // Fire the task
    Task.Factory.StartNew(() => 
    {
        // TODO: the task goes here
        // Remark: Ensure you handle exceptions
    });

    // return immediately
    return Json(
        new { Message = "Task started" }, 
        JsonRequestBehavior.AllowGet
    );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消