4

Im trying to implement a custom Url Builder with Sitecore 9.3.I can't seem to figure out how to patch it. I made a custom url builder that implements BaseUrlBuilder but when I try to patch it in I get:

Unable to cast object of type 'AmeriGasTransform.Foundation.ItemResolution.CustomItemUrlBuilder' to type 'Sitecore.Links.UrlBuilders.ItemUrlBuilder'.

Here is the current state of my patch file:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <links>
        <itemUrlBuilder type="MySolution.Foundation.ItemResolution.CustomItemUrlBuilder,MySolution" patch:instead="*[@type='Sitecore.Links.UrlBuilders.ItemUrlBuilder, Sitecore.Kernel']">
            <param type="Sitecore.Links.UrlBuilders.DefaultItemUrlBuilderOptions, Sitecore.Kernel" desc="defaultOptions">
                <alwaysIncludeServerUrl ref="links/urlBuilder/alwaysIncludeServerUrl"/>
                <languageEmbedding ref="links/urlBuilder/languageEmbedding"/>
                <languageLocation ref="links/urlBuilder/languageLocation"/>
                <lowercaseUrls ref="links/urlBuilder/lowercaseUrls"/>
                <encodeNames ref="links/urlBuilder/encodeNames"/>
                <useDisplayName ref="links/urlBuilder/useDisplayName"/>
                <addAspxExtension>false</addAspxExtension>
                <siteResolving>true</siteResolving>
                <shortenUrls>true</shortenUrls>
            </param>
        </itemUrlBuilder>
    </links>
</sitecore>
3
  • 1
    Try inheriting from Sitecore.Links.UrlBuilders.ItemUrlBuilder and override the Build(item, options) method. BaseUrlBuilder doesn't appear to be registered in Services Config so this still does not appear to be using the IOC container.
    – jammykam
    Jul 21 '20 at 3:35
  • Can you add that as an answer? That was it. Jul 21 '20 at 17:25
  • Thanks, answer added, and updated - BuildUrl() would be probably a better method to override just for the custom link generation part.
    – jammykam
    Jul 24 '20 at 20:37
3

The update in Sitecore 9.3 adds the new itemUrlBuilder configuration to control how item and media URLs are built. Unfortunately, this in not wired into the IoC container (unlike the LinkManager itself for example). The Sitecore documentation states:

The default Sitecore implementation of the BaseLinkManager class uses the Sitecore.Links.LinkProvider class. The LinkProvider class uses the ItemUrlBuilder class to generate item URLs.

If you take a look at the default implementation of the LinkProvider class you will see that the Initialize method uses the Sitecore Config Factory to create the itemUrlBuilder object, which is of type ItemUrlBuilder (and not of type BaseUrlBuilder)

Since the sitecore/links/itemUrlBuilder is indirectly called via the LinkProvider, you should instead inherit from Sitecore.Links.UrlBuilders.ItemUrlBuilder and override the BuildUrl(item, options) method.

public class CustomItemUrlBuilder : Sitecore.Links.UrlBuilders.ItemUrlBuilder
{
    public override string BuildUrl(Item item, ItemUrlBuilderOptions options)
    {
        if (true) {
            // my custom url builder code
        } else {
            return base.BuildUrl(item, options);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.