/// <summary> /// Set up the localized routes for the controller model. /// It uses the [LocalizationRoute] attributes and if no attributes are found it uses culture/controllerName. /// </summary> /// <param name="controllerModel"></param> public void AddControllerRoutes(ControllerModel controllerModel) { string controllerName = controllerModel.ControllerName; // If the controller is the default controller then add the "/", "/culture" routes. // If we don't do this then "/" or "/culture" would throw 404. // Instead /Default would be the only way to access the default controller. if (controllerName.Equals(LocalizationRouteDataHandler.DefaultController, StringComparison.Ordinal)) { // Set up the "/", "/culture1", "/culture2" route templates for all supported cultures. foreach (var kvp in LocalizationRouteDataHandler.SupportedCultures) { string template = LocalizationRouteDataHandler.DefaultCulture == kvp.Key ? "" : kvp.Key; AttributeRouteModel defaultRoute = new AttributeRouteModel(); defaultRoute.Template = template; AddAttributeRouteModel(controllerModel.Selectors, defaultRoute); } } LocalizationRouteDataHandler.AddControllerRouteData(controllerName, LocalizationRouteDataHandler.DefaultCulture, controllerName); // Create the route for the controller to /Default. // Since DefaultController also should be reachable by /Default. // This adds the /Default route template to controllerModel.Selectors so it is reachable by both / and /Default. AttributeRouteModel controllerRoute = new AttributeRouteModel(); controllerRoute.Template = controllerModel.ControllerName; AddAttributeRouteModel(controllerModel.Selectors, controllerRoute); AddControllerLocalizedRoutes(controllerModel); }
/// <summary> /// Add the localized routes for the controller model /// </summary> /// <param name="controllerModel"></param> public void AddControllerLocalizedRoutes(ControllerModel controllerModel) { // Get all the [LocalizationRoute] Attributes from the controller var controllerLocalizations = controllerModel.Attributes.OfType <LocalizationRouteAttribute>().ToList(); string controllerName = controllerModel.ControllerName; // Keep track of which cultures did not have a [LocalizationRoute] attribute so they can have one added programmatically. HashSet <string> notFoundCultures = LocalizationRouteDataHandler.SupportedCultures.Select(kvp => kvp.Key).ToHashSet(); notFoundCultures.Remove(LocalizationRouteDataHandler.DefaultCulture); // Loop over all [LocalizationRoute] attributes foreach (LocalizationRouteAttribute attribute in controllerLocalizations) { string template = attribute.Culture; // If the attributeRoute isn't empty then we use the route name if (!String.IsNullOrEmpty(attribute.Route)) { // Add / if the route doesn't start with / // Otherwise the template would be {culture}{route}. // Instead of {culture}/{route} if (!attribute.Route.StartsWith("/")) { template += "/"; } template += attribute.Route; } // If attribute.Route is empty then we use the controller name so it's not an empty name. else { template += "/" + controllerName; } AttributeRouteModel localRoute = new AttributeRouteModel(); localRoute.Template = template; AddAttributeRouteModel(controllerModel.Selectors, localRoute); // Add the route to the localizations dictionary LocalizationRouteDataHandler.AddControllerRouteData(controllerName, attribute.Culture, template); // Remove it from the not Found Cultures list since the culture had a [LocalizationRoute] attribute. notFoundCultures.Remove(attribute.Culture); } // Add the remaining cultures that didn't have [LocalizationRoute] attributes. foreach (string culture in notFoundCultures) { string template = culture; if (!controllerName.Equals(LocalizationRouteDataHandler.DefaultController, StringComparison.CurrentCultureIgnoreCase)) { template += "/" + controllerName; } AttributeRouteModel localRoute = new AttributeRouteModel(); localRoute.Template = template; AddAttributeRouteModel(controllerModel.Selectors, localRoute); LocalizationRouteDataHandler.AddControllerRouteData(controllerName, culture, template); } }