0

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.

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

1 Answer 1

2

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
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.