Skip to content

[Suggestion] Make DefaultLinkGenerator public and non sealed #25121

@BrightSoul

Description

@BrightSoul

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:

public class MyLinkGenerator : DefaultLinkGenerator
{
    public override string GetPathByAddress<TAddress>(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<LinkGenerator>();
var myLinkGenerator = new LocalizedLinkGenerator(defaultLinkGenerator);
services.AddSingleton<LinkGenerator>(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. It's almost like you didn't even consider people might want to do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-very-fewThis issue impacts very few customersarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-routinginvestigateseverity-majorThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions