gpt4 book ai didi

c# - 来自默认 ASP.NET MVC 新项目的异步操作的解释?

转载 作者:行者123 更新时间:2023-11-30 12:58:46 25 4
gpt4 key购买 nike

<分区>

在 Visual Studio 2013 中创建了一个新的 ASP.NET MVC 项目(我猜这是 MVC 4?)

新项目的默认/模板/生成代码包含一个帐户 Controller 。在该 Controller 中是 async Task<ActionResult>操作,但我并不真正理解它们,或者为什么要在同步代码上使用它们。

public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}

// If we got this far, something failed, redisplay form
return View(model);
}

使此调用异步有什么好处?为什么要遵循这种模式而不是使其成为常规 ActionResult方法?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com