1

How does MVC handle controller actions that return a task. ie what is the difference between

public async Task<ActionResult> MyAction()

and

public ActionResult MyAction()

in the way that MVC process it (I do not want an explanation of the difference between these methods in general, only in the context of how the result of these methods is used to create the view for example)

Edit: I don't see any difference in the views that use it. ie there is no indication that it is handle differently. How does MVC handle it internally?

5
  • 2
    Lots of docs on this... asp.net/mvc/tutorials/mvc-4/… Commented Apr 16, 2014 at 21:39
  • If you're using Tasks, you should really also be using the async keyword (you may also need to derive from the AsyncController class). Commented Apr 16, 2014 at 21:42
  • @martin_costello I am talking about then AccountController for example in MVC 5 where it is not an AsyncController Commented Apr 16, 2014 at 21:51
  • Didn't you intend to write public async Task<ActionResult> MyAction()? Commented Apr 17, 2014 at 9:51
  • @MatthiasMeid yes sorry... It was late at night ;) Commented Apr 17, 2014 at 9:53

1 Answer 1

2

Internally, MVC will (asynchronously) wait for the returned task to complete before sending the response.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you elaborate a little on this? In particular, once the framework has determined that a particular request routes to an asynchronous action, how does it wait on that action without then blocking the request context's thread? Or is the MVC handler fundamentally asynchronous until it invokes a synchronous action?
MVC controllers can be handled either synchronously or asynchronously. When I say it "will (asynchronously) wait", I mean that it will not send the response until the task completes, in a way that does not block the thread.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.