dotnet / dotnet/aspnetcore

Routing system should introduce some abstraction for converting route parameters to strings.

Open
#58,416 0 comments 0 reactions 0 assignees View on GitHub
area-routing
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

### Is your feature request related to a problem? Please describe the problem.

I am working with typed ids:
```
public readonly record struct FooId(Guid Value);
```

If I use this value as a route parameter value and do not override `ToString` [it is converted](https://github.com/dotnet/aspnetcore/blob/049814ca468cad1ea1e29412e0aa3eea182a63c1/src/Http/Routing/src/Template/TemplateBinder.cs#L554) to string as `FooId { Value = '....' }`. That is obviously not what I want to have in a generated URL.

IMO, framework should not force my types to have a certain behavior. Writing parameter value to a URL is effectively a serialization, hence there should be the same level of flexibility/extensibility.

While framework offers outbound parameter transformers, they are not connected to the types of parameter values.

### Describe the solution you'd like

I'd like ASP.NET to introduce a specific interface:
```
public interface IRouteParaemterValueStringConverter
{
string Convert(TParameterValue value);
}
```

Its default implementation can just convert to string.

### Additional context

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.