public static Route Map(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)
        {
            var route = routes.MapRoute(name, url, defaults, constraints, namespaces);

            route.SetRouteName(name);
            var normalized = new NormalizeRoute(route);

            routes.Remove(route);
            routes.Add(name, normalized);

            return(route);
        }
        public void GetVirtualPath_WithAppendTrailingSlashForRootAndQueryString_ReturnsSingleSlash()
        {
            // Arrange
            var originalRoute = new Mock <RouteBase>();

            originalRoute.Setup(r => r.GetVirtualPath(null, null)).Returns(new VirtualPathData(originalRoute.Object, "/?foo=bar"));
            var normalizeRoute = new NormalizeRoute(originalRoute.Object, requireLowerCase: false, appendTrailingSlash: true);

            // Act
            var url = normalizeRoute.GetVirtualPath(null, null);

            // Assert
            Assert.Equal("/?foo=bar", url.VirtualPath);
        }
        public void GetVirtualPath_WithDefaultNormalizeRoute_ReturnsLowerCaseUrl()
        {
            // Arrange
            var originalRoute = new Mock <RouteBase>();

            originalRoute.Setup(r => r.GetVirtualPath(null, null)).Returns(new VirtualPathData(originalRoute.Object, "/Foo/Bar"));
            var normalizeRoute = new NormalizeRoute(originalRoute.Object);

            // Act
            var url = normalizeRoute.GetVirtualPath(null, null);

            // Assert
            Assert.Equal("/foo/bar", url.VirtualPath);
        }
        public void GetVirtualPath_WithLowerCasingFalse_ReturnsOriginalCasing()
        {
            // Arrange
            var originalRoute = new Mock <RouteBase>();

            originalRoute.Setup(r => r.GetVirtualPath(null, null)).Returns(new VirtualPathData(originalRoute.Object, "/Foo/Bar"));
            var normalizeRoute = new NormalizeRoute(originalRoute.Object, requireLowerCase: false, appendTrailingSlash: false);

            // Act
            var url = normalizeRoute.GetVirtualPath(null, null);

            // Assert
            Assert.Equal("/Foo/Bar", url.VirtualPath);
        }
Exemple #5
0
        public static void MapSeoRoutes(this RouteCollection routes)
        {
            var pageRoute =
                new NormalizeRoute(
                    new PageRoute(
                        Constants.PageRoute,
                        new RouteValueDictionary
            {
                { "controller", "Page" },
                { "action", "DisplayPageAsync" },
                //{ Constants.Language, UrlParameter.Optional }
            },
                        new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() },
                { Constants.Page, new PageRouteConstraint() }
            },
                        new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                        new MvcRouteHandler()));

            var itemRoute =
                new NormalizeRoute(
                    new ItemRoute(
                        Constants.ItemRoute,
                        new RouteValueDictionary
            {
                { "controller", "Product" },
                { "action", "ProductByKeywordAsync" },
                //{ Constants.Language, UrlParameter.Optional }
            },
                        new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() },
                { Constants.Category, new CategoryRouteConstraint() },
                { Constants.Item, new ItemRouteConstraint() }
            },
                        new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                        new MvcRouteHandler()));

            var categoryRoute =
                new NormalizeRoute(
                    new CategoryRoute(
                        Constants.CategoryRoute,
                        new RouteValueDictionary
            {
                { "controller", "Collections" },
                { "action", "GetCollectionByKeywordAsync" },
                //{ Constants.Language, UrlParameter.Optional },
                { Constants.Tags, UrlParameter.Optional },
            },
                        new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() },
                { Constants.Category, new CategoryRouteConstraint() }
            },
                        new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                        new MvcRouteHandler()));

            var storeRoute =
                new NormalizeRoute(
                    new StoreRoute(
                        Constants.StoreRoute,
                        new RouteValueDictionary {
                { "controller", "Home" }, { "action", "Index" }
            },
                        new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() }
            },
                        new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                        new MvcRouteHandler()));

            //Legacy redirects
            routes.Redirect(r => r.MapRoute("old_Collection", string.Format("collections/{{{0}}}", Constants.Category))).To(categoryRoute,
                                                                                                                            x =>
            {
                //Expect to receive category code
                if (x.RouteData.Values.ContainsKey(Constants.Category))
                {
                    var client   = ClientContext.Clients.CreateBrowseClient();
                    var store    = SiteContext.Current.Shop.StoreId;
                    var language = SiteContext.Current.Language;
                    var category = Task.Run(() => client.GetCategoryByCodeAsync(store, language, x.RouteData.Values[Constants.Category].ToString())).Result;
                    if (category != null)
                    {
                        var model = category.AsWebModel();
                        return(new RouteValueDictionary {
                            { Constants.Category, model.Outline }
                        });
                    }
                }
                return(null);
            });


            var defaultRoute = new NormalizeRoute(
                new Route(string.Format("{0}/{{controller}}/{{action}}/{{id}}", Constants.StoreRoute),
                          new RouteValueDictionary {
                { "id", UrlParameter.Optional },
                { "action", "Index" }
            },
                          new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() }
            },
                          new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                          new MvcRouteHandler()));

            routes.Add("Page", pageRoute);
            routes.Add("Item", itemRoute);
            routes.Add("Category", categoryRoute);
            routes.Add("Store", storeRoute);

            //Other actions
            routes.Add("RelativeDefault", defaultRoute);
        }
Exemple #6
0
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("virto/services/{MyJob}.svc/{*pathInfo}");
            routes.IgnoreRoute("virto/dataservices/{MyJob}.svc/{*pathInfo}");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute(".html");

            routes.MapRoute(
                "FailWhale",
                "FailWhale/{action}/{id}", new { controller = "Error", action = "FailWhale", id = UrlParameter.Optional });

            routes.MapRoute(
                "Assets",
                "asset/{*path}",
                new { controller = "Asset", action = "Index", path = UrlParameter.Optional });

            var itemRoute = new NormalizeRoute(
                new ItemRoute(Constants.ItemRoute,
                              new RouteValueDictionary
            {
                { "controller", "Catalog" },
                { "action", "DisplayItem" },
                { Constants.Language, UrlParameter.Optional }
            },
                              new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() },
                { Constants.Category, new CategoryRouteConstraint() },
                { Constants.Item, new ItemRouteConstraint() }
            },
                              new RouteValueDictionary {
                { "namespaces", new[] { "Initial.Web.Controllers" } }
            },
                              new MvcRouteHandler()));

            var categoryRoute = new NormalizeRoute(
                new CategoryRoute(Constants.CategoryRoute,
                                  new RouteValueDictionary
            {
                { "controller", "Catalog" },
                { "action", "Display" },
                { Constants.Language, UrlParameter.Optional }
            },
                                  new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() },
                { Constants.Category, new CategoryRouteConstraint() }
            },
                                  new RouteValueDictionary {
                { "namespaces", new[] { "Initial.Web.Controllers" } }
            },
                                  new MvcRouteHandler()));

            var storeRoute = new NormalizeRoute(
                new StoreRoute(Constants.StoreRoute,
                               new RouteValueDictionary
            {
                { "controller", "Home" },
                { "action", "Index" }
            },
                               new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() }
            },
                               new RouteValueDictionary {
                { "namespaces", new[] { "Initial.Web.Controllers" } }
            },
                               new MvcRouteHandler()));

            routes.Add("Item", itemRoute);
            routes.Add("Category", categoryRoute);
            routes.Add("Store", storeRoute);

            //Legacy redirects
            //routes.Redirect(r => r.MapRoute("old_Category", string.Format("c/{{{0}}}", Constants.Category))).To(categoryRoute);
            //routes.Redirect(r => r.MapRoute("old_Item", string.Format("p/{{{0}}}", Constants.Item))).To(itemRoute,
            //    x =>
            //    {
            //        //Resolve item category dynamically
            //        if (x.RouteData.Values.ContainsKey(Constants.Item))
            //        {
            //            var item = CatalogHelper.CatalogClient.GetItem(x.RouteData.Values[Constants.Item].ToString(), bycode: true);
            //            if (item != null)
            //            {
            //                return new RouteValueDictionary { { Constants.Category, item.GetItemCategoryRouteValue() } };
            //            }
            //        };
            //        return null;
            //    });

            var defaultRoute = new NormalizeRoute(new Route(string.Format("{{{0}}}/{{controller}}/{{action}}/{{id}}", Constants.Language),
                                                            new RouteValueDictionary {
                { "id", UrlParameter.Optional }, { "action", "Index" }
            },
                                                            new RouteValueDictionary {
                { Constants.Language, new LanguageRouteConstraint() }
            },
                                                            new RouteValueDictionary {
                { "namespaces", new[] { "Initial.Web.Controllers" } }
            },
                                                            new MvcRouteHandler()));

            //Other actions
            routes.Add("Default", defaultRoute);

            //Needed for some post requests
            routes.MapRoute(
                "Default_Fallback",           // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new
            {
                action = "Index",
                id     = UrlParameter.Optional
            },     // Parameter defaults
                new[] { "Initial.Web.Controllers" });
        }
        /// <summary>
        /// Registers the routes.
        /// </summary>
        /// <param name="routes">The routes.</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("virto/services/{MyJob}.svc/{*pathInfo}");
            routes.IgnoreRoute("virto/dataservices/{MyJob}.svc/{*pathInfo}");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute(".html");

            //Ignore all calls to bundles
            routes.IgnoreRoute("bundles/{*pathInfo}");
            //Ignore all calls to areas (Areas supposed to serve own routes)
            routes.IgnoreRoute("areas/{*pathInfo}");

            //This makes sure index.html (or any other deafult document is opened for empty url
            routes.IgnoreRoute("");

            routes.MapRoute(
                "FailWhale",
                "FailWhale/{action}/{id}", new { controller = "Error", action = "FailWhale", id = UrlParameter.Optional });

            routes.MapRoute(
                "Assets",
                "asset/{*path}",
                new { controller = "Asset", action = "Index", path = UrlParameter.Optional });

            var itemRoute = new NormalizeRoute(
                new ItemRoute(Constants.ItemRoute,
                              new RouteValueDictionary
            {
                { "controller", "Catalog" },
                { "action", "DisplayItem" },
                { Constants.Language, UrlParameter.Optional }
            },
                              new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() },
                { Constants.Category, new CategoryRouteConstraint() },
                { Constants.Item, new ItemRouteConstraint() }
            },
                              new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                              new MvcRouteHandler()));

            var categoryRoute = new NormalizeRoute(
                new CategoryRoute(Constants.CategoryRoute,
                                  new RouteValueDictionary
            {
                { "controller", "Catalog" },
                { "action", "Display" },
                { Constants.Language, UrlParameter.Optional }
            },
                                  new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() },
                { Constants.Category, new CategoryRouteConstraint() }
            },
                                  new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                                  new MvcRouteHandler()));

            var storeRoute = new NormalizeRoute(
                new StoreRoute(Constants.StoreRoute,
                               new RouteValueDictionary
            {
                { "controller", "Home" },
                { "action", "Index" }
            },
                               new RouteValueDictionary
            {
                { Constants.Language, new LanguageRouteConstraint() },
                { Constants.Store, new StoreRouteConstraint() }
            },
                               new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                               new MvcRouteHandler()));

            routes.Add("Item", itemRoute);
            routes.Add("Category", categoryRoute);
            routes.Add("Store", storeRoute);

            //Legacy redirects
            routes.Redirect(r => r.MapRoute("old_Category", string.Format("c/{{{0}}}", Constants.Category))).To(categoryRoute,
                                                                                                                x =>
            {
                //Expect to receive category code
                if (x.RouteData.Values.ContainsKey(Constants.Category))
                {
                    var category = CatalogHelper.CatalogClient.GetCategory(x.RouteData.Values[Constants.Category].ToString());
                    if (category != null)
                    {
                        return(new RouteValueDictionary {
                            { Constants.Category, category.CategoryId }
                        });
                    }
                }
                return(null);
            });
            routes.Redirect(r => r.MapRoute("old_Item", string.Format("p/{{{0}}}", Constants.Item))).To(itemRoute,
                                                                                                        x =>
            {
                //Resolve item category dynamically
                //Expect to receive item code
                if (x.RouteData.Values.ContainsKey(Constants.Item))
                {
                    var item = CatalogHelper.CatalogClient.GetItemByCode(x.RouteData.Values[Constants.Item].ToString(), StoreHelper.CustomerSession.CatalogId);
                    if (item != null)
                    {
                        return(new RouteValueDictionary {
                            { Constants.Category, item.GetItemCategoryRouteValue() }
                        });
                    }
                }
                return(null);
            });

            var defaultRoute = new NormalizeRoute(new Route(string.Format("{{{0}}}/{{controller}}/{{action}}/{{id}}", Constants.Language),
                                                            new RouteValueDictionary {
                { "id", UrlParameter.Optional }, { "action", "Index" }
            },
                                                            new RouteValueDictionary {
                { Constants.Language, new LanguageRouteConstraint() }
            },
                                                            new RouteValueDictionary {
                { "namespaces", new[] { "VirtoCommerce.Web.Controllers" } }
            },
                                                            new MvcRouteHandler()));

            //Other actions
            routes.Add("Default", defaultRoute);

            //Needed for some post requests
            routes.MapRoute(
                "Default_Fallback",           // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new
            {
                action = "Index",
                id     = UrlParameter.Optional
            },     // Parameter defaults
                new[] { "VirtoCommerce.Web.Controllers" });
        }