dotnet / dotnet/aspnetcore

CreatedAtAction does not sanitize action name and Async suffix fails. `nameof(...)` fails.

Open
#64,027 2 comments 1 reaction 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

CreateAtAction does not sanitize action name and therefore if one uses `CreatedAtAction(nameof(MyRouteAsync), ...)` it fails as it cannot find proper route internally. I think it is a bug and the `actionName` argument should be sanitized the same way the method name is sanitized to build internal routes, so `nameof` should work. Otherwise hardcoded string needs to be used or custom-made wrappers stripping Async from nameof need be used.

### Expected Behavior

In my opinion the CreatedAtAction should preprocess the actionName using all the rules it does to build internal route tree, strip Async suffixes etc, so `nameof()` operator will work for any kind of action names.

### Steps To Reproduce

Let's say I have such endpoint:
```csharp
[HttpPost]
public async Task> CreateRuleAsync(
[FromBody] RuleDefinitionV1 ruleDefinitionV1,
[FromServices] IRulesService rulesService,
[FromServices] TimeProvider timeProvider,
[FromServices] IIdGenerator idGenerator,
CancellationToken cancellationToken)
...
```

and then I use such ActionResult in another endpoint:

```csharp
return CreatedAtAction(
nameof(CreateRuleAsync),
new { id = createdRule.Id.Value },
createdRule.AsRuleDefinitionV1());
```

it will fail with exception saying „No route matches the supplied values.”

If you'd try to hardcode name without Async suffix it will work:

```csharp
return CreatedAtAction(
"CreateRule",
new { id = createdRule.Id.Value },
createdRule.AsRuleDefinitionV1());
```

### Exceptions (if any)

System.InvalidOperationException: „No route matches the supplied values.”

### .NET Version

8.0.414 and 10.0.100-rc.1.25451.107

### Anything else?

_No response_

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.