Esempio n. 1
0
        /// <summary>
        /// Register all routes defined in the RouteMappingConfiguration
        /// </summary>
        /// <param name="routes">RouteCollection to add routes</param>
        /// <param name="config">Routes to register</param>
        public static void RegisterRoutes(RouteCollection routes, RouteMappingConfiguration config)
        {
            if (config == null)
            {
                throw new NullReferenceException("Route mapping configuration not defined.");
            }
            using (routes.GetWriteLock())
            {
                foreach (RouteElement ignoreItem in config.IgnoreRoutes)
                {
                    routes.IgnoreRoute(ignoreItem.Url);
                }

                foreach (RouteElement item in config.Routes)
                {
                    var lowerCaseOnly = item.LowerCaseOnly == null ? config.Routes.LowerCaseOnly : item.LowerCaseOnly.Value;

                    var route = new StrictRoute(item.Url, new MvcRouteHandler(), lowerCaseOnly);
                    route.Defaults = new RouteValueDictionary();
                    route.Defaults.Add("Controller", item.Controller);
                    route.Defaults.Add("Action", item.Action);
                    if (item.Defaults != null)
                    {
                        foreach (string key in item.Defaults.AllKeys)
                        {
                            route.Defaults.Add(key, item.Defaults[key].Value);
                        }
                    }
                    if (item.Constraints != null)
                    {
                        route.Constraints = new RouteValueDictionary();
                        foreach (string key in item.Constraints.AllKeys)
                        {
                            route.Constraints.Add(key, new RegexConstraint(item.Constraints[key].Value));
                        }
                    }
                    if (item.Namespace != null)
                    {
                        if (route.DataTokens == null)
                        {
                            route.DataTokens = new RouteValueDictionary();
                        }
                        route.DataTokens["Namespaces"] = new string[] { item.Namespace };

                    }
                    routes.Add(route);
                }
            }
        }
Esempio n. 2
0
        private RouteCollection GetTestCollection()
        {
            StrictRoute route1 = new StrictRoute("", new MvcRouteHandler());
            route1.Defaults = new RouteValueDictionary();
            route1.Defaults.Add("controller", "Home");
            route1.Defaults.Add("action", "Index");

            StrictRoute route2 = new StrictRoute("about", new MvcRouteHandler());
            route2.Defaults = new RouteValueDictionary();
            route2.Defaults.Add("controller", "Home");
            route2.Defaults.Add("action", "About");

            StrictRoute route3 = new StrictRoute("category/{category}/{page}", new MvcRouteHandler());
            route3.Defaults = new RouteValueDictionary();
            route3.Defaults.Add("controller", "Categories");
            route3.Defaults.Add("action", "Detail");
            route3.Defaults.Add("page", 0);
            route3.Constraints = new RouteValueDictionary();
            route3.Constraints.Add("page", @"\d+");

            StrictRoute route4 = new StrictRoute("contact/", new MvcRouteHandler());
            route4.Defaults = new RouteValueDictionary();
            route4.Defaults.Add("controller", "Feedback");
            route4.Defaults.Add("action", "Contact");

            //Build the route Collection
            RouteCollection collection = new RouteCollection()
            {
                route1, route2, route3, route4
            };

            return collection;
        }