-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discussion] Async suffix for controller action names will be trimmed by default #8998
Comments
Maybe you should also update the code sample in the reference. Best wishes~ |
Why? Adding magic to change names seems like a source of bugs. There's no reason to use the |
@manigandham See the linked issue for details. The code fix to make a method asynchronous adds the Note that there's not really any magic going on. The generated action name will just omit “Async” at the end. And remember that the framework already does something similar for controller names. |
Meaningful conventions are not magic. Its been very useful in situations like "Controller", "Attribute" etc |
In case there is an |
Wouldn't it be better to update the code fix itself? The |
I agree with @manigandham. I don't believe .NET (or any framework in general) should be driven by "bugs" in its code fixes, but the other way. |
what if you have two methods with the same names; one is async and one is not? Isn't it strange to have to rename the synchronous method to something completely different? (But maybe i'm just not getting it ;P) |
@BlueMarmalade If you already have two separate actions with the same name but one is As for what happens if you have both, I would assume that they both take the same route and as such the first action will win. |
I'd expect an
to have it explictly. As of today, you had to do the exact opposite to reach the goal
which is counter intuitive since most actions will be async. |
No. In fact, we're talking about adding more sync overloads for APIs that were 100% async before. Async is viral and not all code can be async for practical reasons. On top, even if new tech could be a 100% async, the overwhelming majority of APIs and techologies are sync. It's an important part of the developer experience to have consistency. We found that consistency across multiple technologies is way more important than a local optimum. The reason being that most developers have to write code in various areas and not having to deal with different (or even conflicting) guidance is important. I agree with the issue opener: MVC should ignore the Async suffix. |
FWIW, while I agree with the behavior here is likely the wanted one most of the time, I just hit in in practice on some sample apps that had
Can we improve this 2-matching-routes scenario (which worked before and now breaks) by alerting the user in some way before having to specifically hit one of the duplicate routes at runtime and debug what's breaking? |
In addition to the duplicate scenario above, this simple case breaks: <a asp-controller="Test" asp-action="MyActionAsync">Link Text</a> public async Task<IActionResult> MyActionAsync() { } Why should this break? It's only being handled on one side of the fence. And again, users will only find this out at runtime and only if they go test every route. |
This was broken due to a breaking route mapping change in ASP.NET Core 3, see dotnet/aspnetcore#8998 for details. I also differentiated the messages so it's less confusing.
Having this option wouldn't hurt I just don't think it should be imposed out of the box. The original issue is easily resolved by changing People should be left to their own naming conventions without interruption. Strongly typed areas, controllers and viewsThere are similar issues that have been around a while that I think also need addressing with the UrlHelper and ControllerBase.
Also view resolution:
|
I disagree. The default path ("pit of success") should generate in solutions that follow the standard .NET naming conventions. Of course people should be able to enable whatever naming conventions they want. But I assert that following the common conventions shouldn't require configuration changes because we shouldn't penalize folks for doing the right thing. A successful framework cannot be neutral w.r.t naming conventions. The value is in consistency. |
As @pflannery mentions, this turns out to be a massive headache when using I'm very much not opposed to the new behavior, but it really should do the same transformation on passed-in names when constructing a path/url. |
That one I agree with. |
My teammate waitsted entire day on migration 2.2 --> 3.1 because of this issue. |
i still don't see the point of this change. i'm sure alot of apps have hard coded paths to certain controller methods. changing it like this is just one of those changes that tries to make things easier by hiding/removing stuff by default, but it has little purpose and actually makes things more confusing if anything. |
Yeah, we wasted two days finding out what the issue was while trying to fix the problem in 100 different ways. We multiple Applications that listen to those paths and now we had to change them all. Also, this did not work for us:
Great move Microsoft 👏 |
I think that's what we vaguely decided on part of #14103. |
Thanks for contacting us. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Unfortunately, this still does not work in ASP.NET 5.0.2... Gladly assign me, then I will create a pull request as soon as I get to it. |
Well, it doesn't in 6th LTS version too. It's so disgusting, this bug is undocumented being so obvious to developers. Yesterday I've spent all the day because of this bug. |
My first suggestion would be that we truncate the async suffix respecting the property Are there any better options? Also, I think we should put this issue on the .NET 7.0.0 milestone, as this is imo a very important feature for many users. |
This was broken due to a breaking route mapping change in ASP.NET Core 3, see dotnet/aspnetcore#8998 for details. I also differentiated the messages so it's less confusing.
It is quite strange to find the name of my method has been modified. I'm using reflection to read Action argument types in some middleware with It seems like a change that has good intentions, but is another gotcha that a dev has to keep in mind. I personally don't like my runtime not reflecting the code that I write. |
@LukeBolly At least give us a compiler warning or something right? When it happened to me it wasn't even mentioned in the main documentation, just in a migration-guide. |
It would be extremely welcome if at least the link-generation could be fixed to do the same thing. |
@wagich I feel called out. But I do agree with the sentiment. If it got cut on both sides it makes sure that the code at least works. Still feels like putting some planks over the hole though. |
@Luk164 That was not my intention! |
The change of The reasons leading to this decision regarding the default value are not explained, btw I suppose the reason is related to an overlap of:
To avoid errors I updated my best practice about async into the following: append "Async" to async methods except those related to webapi mapping in ControllerBase specializations example: [ApiController]
[Authorize]
[Route("api/[controller]/[action]")]
public class AuthController : ControllerBase
{
private readonly IAuthService authService;
public AuthController(IAuthService authService)
{
this.authService = authService;
}
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult<LoginDto>> Login([FromBody] LoginRequestDto login) =>
this.CommonResponse(await authService.LoginAsync(login));
} |
As part of addressing #4849, ASP.NET Core MVC will trim the suffix
Async
from action names by default. This affects routing and link generation.Consider
Prior to 3.0, the action will be routeable via
Product/ListAsync
. Link generation would require specifying theAsync
suffix e.g.<a asp-controller="Product" asp-action="ListAsync">List</a>
In 3.0, the action will be routeable via
Product/List
and link generation would require not specifying theAsync
suffix e.g.<a asp-controller="Product" asp-action="List">List</a>
This change does not affect names specified using the
ActionNameAttribute
. This behavior can be disabled by settingMvcOptions.SuppressAsyncSuffixInActionNames
tofalse
as part of the application startup:The text was updated successfully, but these errors were encountered: