What is the difference between public async System.Threading.Tasks.Task<Actionresult> Index() and public Actionresult Index()?

Asked

Viewed 150 times

1

Follows the code:

Example 1 :

public async System.Threading.Tasks.Task<ActionResult> Index()
{

   return View();
}

Example 2:

public ActionResult Index()
{
   return View();
}

Please explain the difference between the 2 with more details possible with get and post.

2 answers

2


The difference is that the first can be executed asynchronously, so it will not block the execution of the application if the method takes too long.

In general you will have a await within this method calling another asynchronously.

As far as I know it makes no difference between the requisition methods.

The question doesn’t give much context, so I can’t answer much more than that.

0

In case it will make no difference and nor should compile because your method does nothing asynchronous.

If you make an asynchronous call in your code the signature of example 1 is required:

public async System.Threading.Tasks.Task<ActionResult> Index()
{
    await MetodoAsync();
    return View();
}

public async Task MetodoAsync()
{
    // faz algo assíncrono
}

In practice, all methods that do something asynchronous need to be marked as async and shall return a Task.

In the case of Controller audience methods, this allows them to be run asynchronously by the framework.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.