protected virtual void TranslateControllerRoute(ControllerModel localizedControllerModel, SelectorModel originalSelectorModel, string template, string culture) { Logger.LogDebug( $"Add translated selector for controller {localizedControllerModel.ControllerName} with \"{template}\" ({culture})."); localizedControllerModel.ThrowIfNotLocalizedModel(RouteTranslationConfiguration.Localizer); if (RouteTranslationConfiguration.ValidateUrl) { ValidateUrl(originalSelectorModel.AttributeRouteModel.Template, template); } if (RouteTranslationConfiguration.AddCultureAsRoutePrefix) { template = RouteTranslationConfiguration.Localizer.ApplyRouteCulturePrefix(template, culture); } SelectorModel localizedSelectorModel = new SelectorModel(originalSelectorModel) { AttributeRouteModel = { Template = template, }, }; localizedControllerModel.Selectors.Add(localizedSelectorModel); RouteTranslationConfiguration.OnControllerRouteTranslated(this, new OnControllerRouteTranslatedEventArgs() { OriginalControllerModel = localizedControllerModel.GetOriginalModel(RouteTranslationConfiguration.Localizer), LocalizedControllerModel = localizedControllerModel, OriginalSelectorModel = originalSelectorModel, LocalizedSelectorModel = localizedSelectorModel, }); }
public void BuildWithControllerActionsSelectionBuildsCorrectProcessor() { DisableOriginalRouteProcessor routeProcessor = CreateRouteProcessor(); routeProcessor.Cultures = new[] { DisableOriginalRouteProcessorBuilderTest.Culture, DisableOriginalRouteProcessorBuilderTest.Culture2, }; ApplicationModel applicationModel = TestApplicationModel.Instance; ControllerModel originalController = applicationModel.Controller1(); ControllerModel translatedController = originalController.CreateLocalizedControllerModel(GetLocalizedRouteMarker(), DisableOriginalRouteProcessorBuilderTest.Culture); foreach (SelectorModel selectorModel in translatedController.GetOriginalModel(GetLocalizedRouteMarker()).Selectors) { translatedController.Selectors.Add(selectorModel); } foreach (ActionModel translatedAction in translatedController.Actions.Take(1)) { foreach (SelectorModel selectorModel in translatedAction.GetOriginalModel(GetLocalizedRouteMarker()).Selectors.Take(1)) { translatedAction.Selectors.Add(selectorModel); } } applicationModel.Controllers.Add(translatedController); translatedController = originalController.CreateLocalizedControllerModel(GetLocalizedRouteMarker(), DisableOriginalRouteProcessorBuilderTest.Culture2); foreach (SelectorModel selectorModel in translatedController.GetOriginalModel(GetLocalizedRouteMarker()).Selectors) { translatedController.Selectors.Add(selectorModel); } foreach (ActionModel translatedAction in translatedController.Actions.Take(1)) { foreach (SelectorModel selectorModel in translatedAction.GetOriginalModel(GetLocalizedRouteMarker()).Selectors) { translatedAction.Selectors.Add(selectorModel); } } applicationModel.Controllers.Add(translatedController); List <RouteSelection> routeSelections = new List <RouteSelection>() { new RouteSelection() { ControllerModel = originalController, ActionModels = originalController.Actions.Take(1).ToList(), }, }; routeProcessor.Process(routeSelections); Assert.IsFalse(originalController.Selectors.First().ActionConstraints.OfType <NeverAcceptActionContraint>().Any()); Assert.IsTrue(originalController.Actions.First() .Selectors.First() .ActionConstraints.OfType <NeverAcceptActionContraint>() .Any()); Assert.IsFalse(originalController.Actions.First() .Selectors.Last() .ActionConstraints.OfType <NeverAcceptActionContraint>() .Any()); }
public void GetOriginalModelForOriginalControllerThrowsException() { ControllerModel controller = TestApplicationModel.Instance.Controller1(); ControllerModel originalController = controller.GetOriginalModel(GetLocalizedRouteMarker()); }
public void SelectWithFilteredNoTranslatedOptionReturnsCorrectSelection4() { ApplicationModel applicationModel = TestApplicationModel.Instance; StaticRouteSelector routeSelector = new StaticRouteSelector(); routeSelector.RouteSelection.Add(new RouteSelection() { ControllerModel = applicationModel.Controller4(), ActionModels = applicationModel.Controller4().Actions, }); routeSelector.RouteSelection.Add(new RouteSelection() { ControllerModel = applicationModel.Controller2(), ActionModels = applicationModel.Controller2().Actions, }); ControllerModel translatedController = applicationModel.Controller4() .CreateLocalizedControllerModel(GetLocalizer(), TranslatedRoutesRouteSelectorBuilderTest.Culture); for (int i = 0; i < translatedController.Actions.Count; i++) { foreach (SelectorModel selectorModel in translatedController.GetOriginalModel(GetLocalizer()).Actions[i].Selectors) { translatedController.Actions[i].Selectors.Add(selectorModel); } } applicationModel.Controllers.Add(translatedController); ControllerModel translatedController2 = applicationModel.Controller2() .CreateLocalizedControllerModel(GetLocalizer(), FilterRouteSelectorTest.Culture); for (int i = 0; i < translatedController2.Actions.Count; i++) { foreach (SelectorModel selectorModel in translatedController2.GetOriginalModel(GetLocalizer()).Actions[i].Selectors) { translatedController2.Actions[i].Selectors.Add(selectorModel); } } applicationModel.Controllers.Add(translatedController2); FilterRouteSelector filterRouteSelector = new FilterRouteSelector(routeSelector) { Controller = "Controller4", Action = "Action1", ActionArguments = new[] { typeof(int) }, Localizer = GetLocalizer(), Cultures = new[] { FilterRouteSelectorTest.Culture }, FilterControllerOrActionWhenNoTranslatedRouteLeft = true, }; ICollection <RouteSelection> routeSelections = filterRouteSelector.Select(applicationModel); Assert.IsTrue(routeSelections.Count == 2); Assert.IsTrue(routeSelections.First().ActionModels.Count == 1); Assert.IsTrue(routeSelections.First().ActionModels.Single().Parameters.Count == 0); Assert.IsTrue(routeSelections.Last().ControllerModel.ControllerName == "Controller2"); Assert.IsTrue(routeSelections.Last().ActionModels.Count == 2); Assert.IsTrue(routeSelections.Last().ActionModels.First().ActionName == "Action1"); Assert.IsTrue(routeSelections.Last().ActionModels.Last().ActionName == "Action2"); }