Esempio n. 1
0
        private RouteModel GetRouteModelForRoute(ITabContext context, MvcRouteBase routeBase,
                                                 Dictionary <int, List <RouteDataMessage> > routeMessages,
                                                 Dictionary <int, Dictionary <int, List <ProcessConstraintMessage> > > constraintMessages)
        {
            RouteModel routeModel = new RouteModel();

            RouteDataMessage routeMessage = SafeFirstOrDefault(routeMessages.GetValueOrDefault(routeBase.GetHashCode()));

            if (routeMessage != null)
            {
                routeModel.Duration = routeMessage.Duration;
                routeModel.IsMatch  = routeMessage.IsMatch;
            }

            MvcRoute route = routeBase as MvcRoute;

            if (route != null)
            {
                routeModel.Area = (route.DataTokens != null && route.DataTokens.ContainsKey("area"))
                                        ? route.DataTokens["area"].ToString() : null;
                routeModel.Url = route.Url;

                // Append localizations if possible
                LocalizationCollectionRoute localizationCollectionRoute = null;
                FieldInfo fieldInfo = route.GetType().GetField("__target");

                if (fieldInfo != null)
                {
                    localizationCollectionRoute = fieldInfo.GetValue(route) as LocalizationCollectionRoute;
                }

                if (localizationCollectionRoute != null)
                {
                    routeModel.Url += " (LocalizationRoute)";
                    routeModel.Url += Environment.NewLine;

                    foreach (LocalizationRoute localizationRoute in
                             localizationCollectionRoute.LocalizedRoutes.OrderBy(x => x.Culture))
                    {
                        routeModel.Url += string.Format(Environment.NewLine + "{0} ({1})", localizationRoute.Url(),
                                                        !string.IsNullOrEmpty(localizationRoute.Culture) ? localizationRoute.Culture : "neutral");
                    }

                    if (!localizationCollectionRoute.LocalizedRoutes.Any())
                    {
                        routeModel.Url += Environment.NewLine + "! No translations exists - this route will not be accessible !";
                    }
                }

                routeModel.RouteData   = ProcessRouteData(route.Defaults, routeMessage);
                routeModel.Constraints = ProcessConstraints(context, route, constraintMessages);
                routeModel.DataTokens  = ProcessDataTokens(route.DataTokens);
            }
            else
            {
                routeModel.Url = routeBase.ToString();
            }

            IRouteNameMixin routeName = routeBase as IRouteNameMixin;

            if (routeName != null)
            {
                routeModel.Name = routeName.Name;
            }

            return(routeModel);
        }