Try our conversational search powered by Generative AI!

Drew Null
Feb 6, 2022
  2120
(0 votes)

Product and category URLs without the catalog slug in Commerce 14 (.NET 5)

This one stumped me the other day, and I couldn't find anything by doing a web or World search.

Problem: How do we exclude the catalog route segment from the URLs of categories and products?

Say you have the following catalog structure:

Catalog Root
    My Catalog // CatalogContent
        My Category // NodeContent
            My Product // ProductContent
                My Variant // VariationContent

By default, the URL to My Variant will look like this:

/my-catalog/my-category/my-variant

But /my-catalog won't resolve to an actual page, so we want to remove it from the URL. Which would render like this:

/my-category/my-variant

Much better. But how do we do this in Commerce 14?

Prior to Commerce 14, this could be done using System.Web.Mvc's RouteTable. But that was killed off in ASP.NET Core, so we need another way.

PartialRouteHandler to the rescue.

This is pretty simple: Create an initialization module and use PartialRouteHandler to register a PageData-to-CatalogContentBase partial router. No custom implementation needed.

Note that how you, say, resolve a catalog to a site, could vary based on the needs of your project. In the example below, we resolve all catalogs to all sites.

using EPiServer;
using EPiServer.Commerce.Catalog.ContentTypes;
using EPiServer.Core;
using EPiServer.Core.Routing;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using Mediachase.Commerce.Catalog;

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class CustomHierarchicalCatalogPartialRouterInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        /* MapDefaultHierarchialRouter [sic] is no longer needed... */
        ////CatalogRouteHelper.MapDefaultHierarchialRouter(false);
        var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
        var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
        var partialRouteHandler = ServiceLocator.Current.GetInstance<PartialRouteHandler>();
        var catalogs = contentLoader.GetChildren<CatalogContentBase>(referenceConverter.GetRootLink());
        foreach (var catalog in catalogs)
        {
            // This implementation will register all catalogs for all sites; if you want it to work differently, do so here--
            partialRouteHandler.RegisterPartialRouter(
                new PartialRouter<PageData, CatalogContentBase>(
                    new HierarchicalCatalogPartialRouter(() => ContentReference.StartPage, catalog, false)));
        }
    }

    public void Uninitialize(InitializationEngine context)
    {
    }
}

And that's it.

Your category, product, and variant pages (and bundles/packages) should resolve without including the catalog slug in the URL. And UrlResolver should now give you the catalog-less URL on the frontend and in the CMS backoffice.

Feb 06, 2022

Comments

Please login to comment.
Latest blogs
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog

Anonymous Tracking Across Devices with Optimizely ODP

An article by Lead Integration Developer, Daniel Copping In this article, I’ll describe how you can use the Optimizely Data Platform (ODP) to...

Daniel Copping | Apr 30, 2024 | Syndicated blog

Optimizely Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024