Try our conversational search powered by Generative AI!

Html Helper extension for Canonical Link does not include the hostname.

Vote:
 

In the EPiServer.Web.Mvc.Html.CanonicalLink function, the returned value does not include the Hostname, which is required for Canonical Links. Thanks!

#254144
Apr 30, 2021 16:47
Vote:
 

Hi John! I recently needed to add canonical links including the hostname as well. I ended up adding an extension method to UrlHelper to handle this:

        public static string CanonicalUrl(this UrlHelper urlHelper, PageData page)
        {
            var resolver = ServiceLocator.Current.GetInstance<UrlResolver>();
            var contentUrl = resolver.GetUrl(page.ContentLink, page.Language.TwoLetterISOLanguageName, new VirtualPathArguments {
                ContextMode = ContextMode.Default,
                ForceAbsolute = true,
                ForceCanonical = true,
            });

            return contentUrl;
        }

 

#254147
Apr 30, 2021 18:30
Vote:
 

There might be more things to check when generating canonical urls. I haven't used that extension for a while (might be obsolete)..

https://github.com/Geta/EPi.Extensions/blob/4f0079a73855c014e8a85e300d1c719eca24bcaf/src/Geta.EPi.Extensions/PageDataExtensions.cs#L218

#255426
May 24, 2021 21:22
* 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.