'Async suffix in a method name now deprecated?
VS2022, Asp.Net MVC .Net 6.0
When a function name ends with "async", the url never works. Cannot figure out why. New routing defaults, new "guidelines" enforced or simply a bug?
//works (example https://localhost:7215/home/getuserlist)
public async Task<IActionResult> GetUserList()
//doesn't work (example https://localhost:7215/home/getuserlistasync)
public async Task<IActionResult> GetUserListAsync()
//works (example https://localhost:7215/routetest)
[Route("routetest")]
public async Task<IActionResult> GetUserListAsync()
Solution 1:[1]
It is indeed a known behavior, albeit poorly documented. You may call it a bug if you'd like.
The original bug report can be found here, very similar to what you've described: https://github.com/dotnet/aspnetcore/issues/4849
The corresponding issue is here: https://github.com/dotnet/docs/issues/14716. The intention is to trim the suffix from the action name, because it upsets routing and link generation. They even propose a workaround:
services.AddMvc(options =>
{
options.SuppressAsyncSuffixInActionNames = false;
});
Although there are some reports that line might not work. Very confusing!
And here is the discussion that sheds some light on why the behavior you are observing isn't fixed yet: https://github.com/dotnet/aspnetcore/issues/8998. There is even a pull request that wasn't merged because there was no agreement on how to properly trim this suffix from the action name without breaking various common routines (think nameof()) and naming conventions: https://github.com/dotnet/aspnetcore/pull/39128.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrei |
