19

Given the following ASP.NET MVC 4 controller action:

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);
    }
}

Do I need to put the "Async" suffix in FooAsync for the action to be run asynchronously?

Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
  • see also https://stackoverflow.com/questions/15951774/does-the-use-of-the-async-suffix-in-a-method-name-depend-on-whether-the-async – Tim Abell Mar 25 '19 at 13:47

2 Answers2

18

Although it's not a requirement, I like to follow convention and see the "...Async" suffix on my asynchronous methods, so I tend to use the ActionName attribute to decorate the controller actions with the non-async name, this way you can have your cake and eat it too :)

[ActionName("Index")]
public async Task<ActionResult> IndexAsync()
{
    return View();
}

I find this works just fine.

Tom Tregenna
  • 1,281
  • 1
  • 13
  • 23
16

No, this is not a requirement.

By convention, you append "Async" to the names of methods that have an Async or async modifier.

You can ignore the convention where an event, base class, or interface contract suggests a different name. For example, you shouldn’t rename common event handlers, such as Button1_Click.

source: MSDN: Asynchronous Programming with Async and Await C# -> Naming Convetions

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • 3
    According to [Using Asynchronous Methods in ASP.NET MVC 4](http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4), "Appending 'Async' is not required but is the convention when writing asynchronous methods." – Stephen Cleary Feb 01 '13 at 21:49
  • @StephenCleary - Thank you for confirming that. The link I provided also points to Naming Conventions. However, not including the word "Async" as a suffix will have no affect on the method, and so it is not necessarily "Required". – Travis J Feb 01 '13 at 21:55
  • @TravisJ: Thanks, Travis, I know about the convention; however, I wanted to know if the suffix was actually *required*. – Marius Schulz Feb 02 '13 at 11:30
  • 2
    @MariusSchulz - I clearly stated "**No, it is not a requirement**". You *do not* have to append the suffix *Async* to the methods. – Travis J Feb 02 '13 at 23:18
  • @TravisJ: Yes, but the source you provided is talking about conventions, which I know about. Anyway, since your answer is correct, I've accepted it. Thanks for your help! – Marius Schulz Feb 03 '13 at 15:53