November Happy Hour will be moved to Thursday December 5th.

Jonas Bergqvist
Apr 29, 2014
  6691
(3 votes)

How to register the partial routing in Commerce

To make a site work in Commerce 7.5, we have to register a partial router that will handle the catalog routing. The partial router, that comes out of the box, is called ‘hierarical catalog partial router’. This partial router needs to be registered during initialization if it’s intended to be used.

Different ways of register the partial route in an initialization module

We should always use an initialization module for the registration of the partial route. If there is no initialization module in the project that fits this purpose, create one.

An initialization module is a class that implements the interface “IInitializableModule”. When used in commerce, the class should also be decorated with the “ModuleDependency” attribute, with it’s type set to Commerce.Initialization.InitializationModule.

[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class Initialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
    }
}

PartialRouteHandler

To register the partial router, we can use the “PartialRouteHandler” inside the “Initialize” method of our initialization module.

We will first get the services “ReferenceConverter”, “IContentLoader”, and “PartialRouteHandler”. Those will be used for getting the catalog root content, and for the registration.

We will get the catalog root link using the reference converter. When we have the content link, it’s easy to get the content using the content loader.

At last we will create a hierarchical catalog partial router. We will in this example set the start page for the route to the start page of the route. This could be changed to be a setting property on the start page type. We will also tell the partial router that it should not use SEO Uri:s when creating links. By changing false to true for the last parameter, SEO Uris would be generated as links for content on the site.

var referenceConverter = context.Locate.Advanced.GetInstance<Mediachase.Commerce.Catalog.ReferenceConverter>();
var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();
var partialRouteHandler = context.Locate.Advanced.GetInstance<PartialRouteHandler>();

var commerceRootContent = contentLoader.Get<Commerce.Catalog.ContentTypes.CatalogContentBase>(referenceConverter.GetRootLink());
var hierarchicalCatalogPartialRouter = new HierarchicalCatalogPartialRouter(() => SiteDefinition.Current.StartPage, commerceRootContent, false);

partialRouteHandler.RegisterPartialRouter(new PartialRouter<Core.PageData, Commerce.Catalog.ContentTypes.CatalogContentBase>(hierarchicalCatalogPartialRouter));

RegisterPartialRouter extension method

There is an extension method in the namespace EPiServer.Web.Routing, that makes the registration a little bit easier. By using the method “RegisterPartialRouter”, we don’t have to use the partial route handler directly.

var referenceConverter = context.Locate.Advanced.GetInstance<Mediachase.Commerce.Catalog.ReferenceConverter>();
var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();

var commerceRootContent = contentLoader.Get<Commerce.Catalog.ContentTypes.CatalogContentBase>(referenceConverter.GetRootLink());
var hierarchicalCatalogPartialRouter = new HierarchicalCatalogPartialRouter(() => SiteDefinition.Current.StartPage, commerceRootContent, false);

RouteTable.Routes.RegisterPartialRouter(hierarchicalCatalogPartialRouter);

MapDefaultHierarchialRouter extension method

In cases where the the catalog content should be set to the catalog root (as in the examples above), another extension method can be used, which is located in the namespace EPiServer.Commerce.Routing. The extension method is called “MapDefaultHierarchialRouter”, and it’s only possible to set the start page, and SEO Uri flag on that one.

CatalogRouteHelper.MapDefaultHierarchialRouter(RouteTable.Routes, () => SiteDefinition.Current.StartPage, false);

There is also an overload that only set the SEO Uri flag. This one will use the SiteDefinition.Current.StartPage as the start page for the partial route.

CatalogRouteHelper.MapDefaultHierarchialRouter(RouteTable.Routes, false);

Using a node as the catalog root for routing

You might want to change the catalog root for the partial routing. In our example site, we have a catalog called “Departmental Catalog”, with a “Departments” node under it. We could change the registration to have Department as the root content by the following code:

[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class Initialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var referenceConverter = context.Locate.Advanced.GetInstance<Mediachase.Commerce.Catalog.ReferenceConverter>();
        var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();

        var departmentCatalog = contentLoader.GetChildren<Commerce.Catalog.ContentTypes.CatalogContent>(referenceConverter.GetRootLink()).First();
        var departmentsNode = contentLoader.GetChildren<Commerce.Catalog.ContentTypes.NodeContent>(departmentCatalog.ContentLink).First();

        var hierarchicalCatalogPartialRouter = new HierarchicalCatalogPartialRouter(() => SiteDefinition.Current.StartPage, departmentsNode, false);

        RouteTable.Routes.RegisterPartialRouter(hierarchicalCatalogPartialRouter);
    }
}
Apr 29, 2014

Comments

Frederik Vig
Frederik Vig Apr 29, 2014 09:32 AM

Don't you need to pass in RootContent for HierarchicalCatalogPartialRouter?

Apr 29, 2014 12:47 PM

Frederik Vig: You had to do that before 7.7.2.74, which was released today. Get the latest package and this will work. It was a bug before that made you send in RootContent. This has been changed to CatalogContentBase.

K Khan
K Khan Apr 30, 2014 03:01 PM

What changes we will need if we will need Filters in URLs? e.g. url/color/black/size/uk10

May 1, 2014 09:13 AM

Khan: Intresting. I would probobly inherit from the HierarchicalCatalogPartialRouter and override some of the methods. This is something I will try out when I got time.

K Khan
K Khan May 8, 2014 04:37 PM

HI Jonas,

I have following urls
url/US/productseokey
url/CA/productseokey
both are pointing to same product.

But get the following error
Exception: Exception in ecf_CatalogNode_UriLanguage: Column 'CatalogNodeId' is constrained to be unique. Value '1' is already present.Column 'CatalogNodeId' is constrained to be unique. Value '1' is already present.Column 'CatalogNodeId' is constrained to be unique. Value '1' is already present.]

I can save same SEOURI key for different languages through caommerce manager and CMS/Edit window

I am wondering, do you have any idea, how can we overcome this limit

Regards
Khurram

K Khan
K Khan Aug 11, 2014 11:16 AM

Issues above have been fixed we can expect those in next in 7.10.1+
http://world.episerver.com/Forum/Developer-forum/EPiServer-Commerce/Thread-Container/2014/7/Routing-for-multilingual-site/

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog