dotnet / dotnet/aspnetcore

[Suggestion] Make DefaultLinkGenerator public and non sealed

Open
#25,121 9 comments 5 reactions 0 assignees View on GitHub
affected-very-few area-mvc enhancement feature-routing investigate severity-major
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

Hi everyone,
I'd like to localize URLs of my application and, in order to do that, my solution is to translate route values for `action` and `controller` before they're actually processed by the `DefaultLinkGenerator`. This is what I'd like to do:

```csharp
public class MyLinkGenerator : DefaultLinkGenerator
{
public override string GetPathByAddress(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString? pathBase = null, FragmentString fragment = default, LinkOptions options = null)
{
//Fiddle with the RouteValueDictionary here

//Then invoke the base method
return base.GetPathByAddress(httpContext, address, values, ambientValues, pathBase, fragment, options);
}
//...
}
```
Since I don't want to change the default url generation logic, I thought this was a simple and effective solution.
It's not viable though, since the `DefaultLinkGenerator` is a `private sealed` class.

Is there a particular reason why it cannot be a normal, overridable public class?

The fact it's a `private sealed` class did not stop me from achieving this solution, in fact I just had to wrap the `DefaultLinkGenerator` with my own implementation. It works but it's way uglier.

In Startup.ConfigureServices:
```
var serviceProvider = services.BuildServiceProvider();
var defaultLinkGenerator = serviceProvider.GetService();
var myLinkGenerator = new LocalizedLinkGenerator(defaultLinkGenerator);
services.AddSingleton(myLinkGenerator);
```
Would you please consider to make the `DefaultLinkGenerator` public and non-sealed?

Thanks.
P.S. Overall, I've found maaaany obstacles when trying to localize URLs. Another is in [issue #16965](https://github.com/dotnet/aspnetcore/issues/16965). It's almost like you didn't even consider people might want to do it.

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.