Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Difference between 'public async Task<IActionResult>' and 'public ActionResult' in MVC

I don't know what is Difference Between Both Methods in Asp.Net Core MVC6

[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditPost(int? id)
{
    if (id == null)
    {
        return NotFound();
    }
    var studentToUpdate = await _context.Students.SingleOrDefaultAsync(s => s.ID == id);
    if (await TryUpdateModelAsync<Student>(
        studentToUpdate,
        "",
        s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
    {
        try
        {
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        catch (DbUpdateException /* ex */)
        {
            //Log the error (uncomment ex variable name and write a log.)
            ModelState.AddModelError("", "Unable to save changes. " +
                "Try again, and if the problem persists, " +
                "see your system administrator.");
        }
    }
    return View(studentToUpdate);
}

And

[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public ActionResult EditPost(int? id)
{
    if (id == null)
    {
        return NotFound();
    }
    var studentToUpdate =  _context.Students.SingleOrDefaultAsync(s => s.ID == id);
    if (TryUpdateModelAsync<Student>(
        studentToUpdate,
        "",
        s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
    {
        try
        {
            _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        catch (DbUpdateException /* ex */)
        {
            //Log the error (uncomment ex variable name and write a log.)
            ModelState.AddModelError("", "Unable to save changes. " +
                "Try again, and if the problem persists, " +
                "see your system administrator.");
        }
    }
    return View();
}

I see that the MVC code now has async but what is the difference. Does one give much better performance than the other? Is it easier to debug problems with one than the other? Should I make changes to other controllers for my application to add Async ?

like image 815
Praful Chauhan Avatar asked Jan 05 '23 14:01

Praful Chauhan


1 Answers

An action method that just returns an ActionResult is inherently synchronous. Therefore, any long running method that is executed within the MVC action will hold the thread and not make it available to serve other web requests. However, when you use async Task<ActionResult> and you call in a method within the action that is long running and asynchronous the thread is freed up and a callback is initiated to take over when the long running method returns.

Having said that, if you do not use async programming within the action method I believe it doesn't make any difference. But if you use EF or any library worth its salt, it will be async.

As a developer, you don't need to do anything special really, other than calling and awaiting the async method, so there is really no value in not using it.

Performance wise you won't really see any big change on Dev but on prod or load testing you will see the gain.

To sum it up, if you see an async implementation, use it, unless you really have to use synchronous for a special purpose.

like image 97
Muqeet Khan Avatar answered Jan 07 '23 04:01

Muqeet Khan