dotnet / dotnet/aspnetcore

Provide an option on FromRouteAttribute that allows decoding the value being bound

Open
#11,544 32 comments 50 reactions 0 assignees View on GitHub
affected-medium area-minimal area-mvc enhancement feature-model-binding severity-minor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

The rights of my users are at `/api/admin/users/{userId}/rights`

And my controller is as simple as

```cs
[Route("/api/admin/users/{userId}/rights")]
public async Task GetRights([FromRoute]string userId)
{
var rights = await _someService.GetRights(userId);
return Ok(rights);
}
```

or

```cs
[HttpPut("{userId}")]
public async Task Update(string userId, [FromBody]UpdateUserViewModel parameters)
{
var user = await _employeService.Update(userId, parameters);
return Ok(user);
}
```

The problem I have is that, the userIds of my users may contains a `/` which is encoded to `%2F` in the Uri. But userId doesn't decode `%2F` so my string contains `%2F`. It's fine for me, I can deal with that.

But the userIds of my users may contains a `+` which is encoded to `%2B` in the Uri. And now, the userId decode the `%2B` to `+` 😲

Currently, I can't use `WebUtility.UrlDecode(userId)` because `userId` may contains a `+` which would be send as `%2B` decoded as `+` and finally to ` `. My only actual solution is to replace `%2F` to `/` which is ugly and does not solve all the possibility : `%252F`

I saw a recommandation to use `[FromQuery]` instead of `[FromRoute]` but it's obvious that if both exist, it's because they have semantic difference.

It seems that's not the first time the problem appears : https://github.com/aspnet/Mvc/issues/4599, https://github.com/aspnet/AspNetCore/issues/2655, https://github.com/aspnet/AspNetCore/issues/4445 and I'd like to know if it's on the roadmap to change this behavior or not.

Could you please this time consider this bug ? I'd be happy to help.

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.