Changing route for variation and category



I am trying to change the routes of variations and categories.

I would like categories to have the following route:{language}/{category1-routesegment}/{category2-routesegment} etc.

I would like variations to have the following route:{language}/{variation-routesegment}/p/{variation-code}

I have done the following:

    public class TestPartialRouter : HierarchicalCatalogPartialRouter
        private readonly IContentLoader _contentLoader;
        private readonly ReferenceConverter _referenceConverter;

        public TestPartialRouter(Func<ContentReference> routeStartingPoint, CatalogContentBase commerceRoot, bool enableOutgoingSeoUri, IContentLoader contentLoader, ReferenceConverter referenceConverter) : base(routeStartingPoint, commerceRoot, enableOutgoingSeoUri)
            _contentLoader = contentLoader;
            _referenceConverter = referenceConverter;

        protected override bool TryGetVirtualPath(HttpContextBase context, CatalogContentBase content, string language, out string virtualPath)
            var variationExists = _contentLoader.TryGet(content.ContentLink, out Variation variation);
            if (variationExists)
                virtualPath = $"{variation.RouteSegment}/dp/{variation.ProductCode}";
                return true;
            return base.TryGetVirtualPath(context, content, language, out virtualPath);

        protected override CatalogContentBase FindNextContentInSegmentPair(CatalogContentBase catalogContent, SegmentPair segmentPair,
            SegmentContext segmentContext, CultureInfo cultureInfo)
            return base.FindNextContentInSegmentPair(catalogContent, segmentPair, segmentContext, cultureInfo);

Currently the routes are correct in the Commerce, but I get a 404 when I access the variation URL. The category URLs are working fine! I've tried a variety of stuff in FindNextContentInSegmentPair, but I haven't been able to hit the variation page as intended.

The route is registered as follows:

var referenceConverter = context.Locate.Advanced.GetInstance<ReferenceConverter>();
var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();
var commerceRootContent = contentLoader.GetChildren<CatalogContent>(referenceConverter.GetRootLink()).FirstOrDefault();
Func<ContentReference> startingPoint = () => ContentReference.IsNullOrEmpty(SiteDefinition.Current.StartPage) ?
                SiteDefinition.Current.RootPage :
RouteTable.Routes.RegisterPartialRouter(new TestPartialRouter(startingPoint, commerceRootContent, false, contentLoader, referenceConverter));

Any help is appreciated, thank you!



Edited, Aug 24, 2019 0:04

That's what your FindNextContentInSegmentPair has to do - it has to return the VariationContent when it hits{language}/{variation-routesegment}/p/{variation-code}

However it'll be tricky because you would need identify the routesegment here, and that'd be a very expensive task unless you hit database directly (which is generally advised against), or if you work it backwardly...  

Aug 26, 2019 7:45

I've tried hardcoding a return of a random variation in my catalogue, but I still get 404:

Maybe I'm not sure what FindNextContentSegmentPair is suppose to do...

Aug 26, 2019 9:21

Does your variation content type have a view associated with it ? 

Aug 26, 2019 10:36

It does yes:

    public class VariationController : ContentController<Variation>
        public ActionResult Index(Variation currentContent)
            var viewModel = new ContentViewModel<Variation>(currentContent);
            return View(viewModel);


@model GenericStore.Models.ViewModels.ContentViewModel<GenericStore.Models.Commerce.Variation>

Aug 26, 2019 13:51

Hi Quan Mai,

It has been a while, but I was hoping you could give your insight on my issue here, since I have not been able to resolve it.

Any idea or suggestion on how to approach here?



Oct 03, 2022 15:41

It's been 3 years so I have no idea what I was talking about. I will try to dig it up but it'll take time 

Oct 04, 2022 10:10
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.