I have an ASP.NET MVC project with some controller, views and actions and so on.
I have heard that I should be able to make my controller actions async without any problems, but I'm really struggling with how to return views in that case.
I have this action called UpdateUser()
which is async, and has some functions that I want to have the await
keyword (more are to be added). After doing these operations, I need to return to a view, like in most controller action:
public async Task Updateuser()
{
ApplicationUser usr = await _userManager.FindByNameAsync(User.Identity.Name);
string name = usr.UserName;
string email = usr.Email;
string UserEmail = name email;
string hash = "";
using (var sha = new System.Security.Cryptography.SHA256Managed())
{
// Convert the string to a byte array first, to be processed
byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(UserEmail);
byte[] hashBytes = sha.ComputeHash(textBytes);
// Convert back to a string, removing the '-' that BitConverter adds
hash = BitConverter
.ToString(hashBytes)
.Replace("-", String.Empty).ToLower();
}
return View();
}
So at the line where it says return View();
my IDE is angry about the return type, because the return type needs to be a `Task.
So how do I return my view, using async methods?
CodePudding user response:
You should use IActionResult to return the view from MVC controller's action. Your method should like the following with the async task:
public async Task<IActionResult> Updateuser()
{
return View();
}