【发布时间】:2021-11-08 13:19:09
【问题描述】:
我有一个 ASP.NET MVC 项目,其中包含一些控制器、视图和操作等。
我听说我应该能够毫无问题地使我的控制器操作异步,但在这种情况下我真的很纠结如何返回视图。
我有一个名为UpdateUser() 的异步操作,并且有一些我希望有await 关键字的功能(更多有待添加)。完成这些操作后,我需要返回一个视图,就像在大多数控制器操作中一样:
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();
}
因此,在显示return View(); 的那一行,我的 IDE 对返回类型很生气,因为返回类型必须是“任务”。
那么如何使用异步方法返回我的视图?
【问题讨论】:
-
我怀疑方法签名应该是
Task<ActionResult>(或Task<IActionResult>)
标签: c# asp.net-mvc