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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With