dotnet / dotnet/aspnetcore

Controller action with `Async` suffix causes issues when resolving url from `IUrlHelper.Action`

Open
#55,967 0 comments 2 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Using the UrlHelperExtension `IUrlHelper.Action(action, controller)` to generate a URL behaves differently based on the naming convention of the action:

If `action` has an `Async` suffix, it seems to not resolve correctly - especially when referring to actions through `nameof(MyActionAsync)` for easy refactoring.

More specifically:
`FooController/BarAction` yields `"api/foo/baraction"` for `Url.Action("BarAction", "Foo")`
`FooController/BarActionAsync` yields `null` for `Url.Action("BarActionAsync", "Foo")`

### Expected Behavior

Either
A) some mention of this behaviour in [the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.urlhelper.action?view=aspnet-mvc-5.2) about the convention of stripping `Async` from the URL-resolver, or
B) verbatim matching on the action name.

### Steps To Reproduce

```csharp
// [Route("api/[controller]")
// class MyController : ControllerBase

// [HttpGet(..., Name = nameof(Foo)]
// ActionResult Foo(string foo) { ... }

Url.Action("Foo", "My", new { foo }) // => "api/my/foo" - OK

// [HttpGet(..., Name = nameof(Bar)]
// ActionResult Bar(string foo) { ... }

Url.Action("Foo", "My", new { foo }) // => null - OK, expected as there is a mismatch in name

// [HttpGet(..., Name = nameof(FooAsync)]
// ActionResult FooAsync(string foo) { ... }

Url.Action("FooAsync", "My", new { foo }) // => null - What happened here?

// [HttpGet(..., Name = nameof(FooAsync)]
// MyController.FooAsync(string foo) { ... }

Url.Action("Foo", "My", new { foo }) // => "api/my/foo" - OK, but Async seems to be stripped somewhere.
```

### Exceptions (if any)

None

### .NET Version

8.0.202

### Anything else?

Output from `dotnet --info`

```
❯ dotnet --info
.NET SDK:
Version: 8.0.202
Commit: 25674bb2f4
Workload version: 8.0.200-manifests.8cf8de6d

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.202\

.NET workloads installed:
There are no installed workloads to display.

Host:
Version: 8.0.3
Architecture: x64
Commit: 9f4b1f5d66

.NET SDKs installed:
3.1.426 [C:\Program Files\dotnet\sdk]
6.0.321 [C:\Program Files\dotnet\sdk]
8.0.202 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.