Mar*_*ulz 15 asp.net-mvc asynchronous async-await asp.net-mvc-4
鉴于以下ASP.NET MVC 4控制器操作:
public async Task<ActionResult> FooAsync()
{
using (var httpClient = new HttpClient())
{
var responseMessage = await httpClient.GetAsync("http://stackoverflow.com");
string response = await responseMessage.Content.ReadAsStringAsync();
return Content(response);
}
}
Run Code Online (Sandbox Code Playgroud)
我是否需要在FooAsync中添加 "Async"后缀才能异步运行操作?
Tom*_*nna 12
虽然这不是必需的,但我喜欢遵循约定并在我的异步方法上看到"... Async"后缀,因此我倾向于使用该ActionName属性来使用非异步名称来装饰控制器操作,这样你就可以拥有你的蛋糕也吃了:)
[ActionName("Index")]
public async Task<ActionResult> IndexAsync()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
我觉得这很好用.
Tra*_*s J 10
不,这不是必需的.
按照惯例,您将"Async"附加到具有Async或async修饰符的方法的名称.
您可以忽略事件,基类或接口契约建议不同名称的约定.例如,您不应重命名常用事件处理程序,例如Button1_Click.