Views: 4910
Number of votes: 3
Average rating:

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

frederik
(By frederik, 4/29/2014 11:32:15 AM)

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

jonas.bergqvist
(By jonas.bergqvist, 4/29/2014 2:47:19 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
(By K Khan , 4/30/2014 5:01:38 PM)

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

jonas.bergqvist
(By jonas.bergqvist, 5/1/2014 11:13:08 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
(By K Khan , 5/8/2014 6:37:42 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
(By K Khan , 8/11/2014 1:16:51 PM)

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.