protected virtual SeoRouteResponse Redirect(SeoEntity entity)
 {
     return(new SeoRouteResponse
     {
         Redirect = true,
         RedirectLocation = entity.SeoPath,
     });
 }
Exemple #2
0
        protected virtual async Task <SeoEntity> FindEntityBySlugPath(string path, WorkContext workContext)
        {
            path = path.Trim('/');

            var slugs    = path.Split('/');
            var lastSlug = slugs.LastOrDefault();

            // Get all SEO records for requested slug and also all other SEO records with different slug and languages but related to the same object
            var allSeoRecords = await GetAllSeoRecordsAsync(lastSlug);

            var bestSeoRecords = GetBestMatchingSeoRecords(allSeoRecords, workContext.CurrentStore, workContext.CurrentLanguage, lastSlug);

            var seoEntityComparer = AnonymousComparer.Create((SeoEntity x) => string.Join(":", x.ObjectType, x.ObjectId, x.SeoPath));
            // Find distinct objects
            var entities = bestSeoRecords
                           .Select(s => new SeoEntity {
                ObjectType = s.ObjectType, ObjectId = s.ObjectId, SeoPath = s.SemanticUrl
            })
                           .Distinct(seoEntityComparer)
                           .ToList();

            // Don't load objects for non-SEO links
            if (workContext.CurrentStore.SeoLinksType != SeoLinksType.None)
            {
                foreach (var group in entities.GroupBy(e => e.ObjectType))
                {
                    await LoadObjectsAndBuildFullSeoPaths(group.Key, group.ToList(), workContext.CurrentStore, workContext.CurrentLanguage);
                }

                entities = entities.Where(e => !string.IsNullOrEmpty(e.SeoPath)).ToList();
            }

            // If found multiple entities, keep those which have the requested SEO path
            if (entities.Count > 1)
            {
                entities = entities.Where(e => e.SeoPath.EqualsInvariant(path)).ToList();
            }

            // If still found multiple entities, give up
            var result = entities.Count == 1 ? entities.FirstOrDefault() : null;

            if (result == null)
            {
                // Try to find a static page
                var page = FindPageBySeoPath(path, workContext);
                if (page != null)
                {
                    result = new SeoEntity
                    {
                        ObjectType     = "Page",
                        SeoPath        = page.Url,
                        ObjectInstance = page,
                    };
                }
            }

            return(result);
        }
Exemple #3
0
        protected virtual SlugRouteResponse View(SeoEntity entity)
        {
            var response = new SlugRouteResponse();

            switch (entity.ObjectType)
            {
            case "Category":
                response.RouteData["action"]     = "CategoryBrowsing";
                response.RouteData["controller"] = "CatalogSearch";
                response.RouteData["categoryId"] = entity.ObjectId;
                break;

            case "CatalogProduct":
                response.RouteData["action"]     = "ProductDetails";
                response.RouteData["controller"] = "Product";
                response.RouteData["productId"]  = entity.ObjectId;
                break;

            case "Vendor":
                response.RouteData["action"]     = "VendorDetails";
                response.RouteData["controller"] = "Vendor";
                response.RouteData["vendorId"]   = entity.ObjectId;
                break;

            case "Page":
                response.RouteData["action"]     = "GetContentPage";
                response.RouteData["controller"] = "StaticContent";
                response.RouteData["page"]       = entity.ObjectInstance;
                break;

            case "Asset":
                response.RouteData["action"]     = "GetThemeAssets";
                response.RouteData["controller"] = "Asset";
                response.RouteData["path"]       = entity.SeoPath;
                break;
            }

            return(response);
        }