4

I implemented localized routes using a ApplicationModelConvention. This works fine as long as I type the urls directly in the browser.

So for example german routes are only expected in german culture. and routes from other languages are returning a 404. Exactly as expected.

But when I use Url.Action doesn't respect the constraints. so for example if i'm on the english domain i get russian links

What am I doing wrong?

my code is here:

services.AddMvc(o =>
{
    o.Conventions.Insert(0, new LocalizedRouteConvention());
})

public class LocalizedRouteConvention : IApplicationModelConvention
{
    public void Apply(ApplicationModel application)
    {
        foreach (var controller in application.Controllers)
        {
            foreach (var action in controller.Actions)
            {
                var attributes = action.Attributes.OfType<RouteAttribute>().ToArray();
                if (!attributes.Any()) return;

                foreach (var attribute in attributes)
                {
                    SelectorModel defaultSelector = action.Selectors.First();

                    foreach (var localizedVersion in GetLocalized(attribute.Template))
                    {
                        if (!action.Selectors.Any(s => s.AttributeRouteModel.Template == localizedVersion.Template))
                        {
                            action.Selectors.Insert(0, new SelectorModel(defaultSelector)
                            {
                                AttributeRouteModel = localizedVersion,
                                ActionConstraints =
                                {
                                    new CultureActionConstraint { Culture = ((LocalizedRouteAttribute) localizedVersion.Attribute).Culture }
                                }
                            });
                        }
                    }
                }
            }
        }
    }
}
Marco
  • 22,856
  • 9
  • 75
  • 124
Boas Enkler
  • 12,264
  • 16
  • 69
  • 143

0 Answers0