antonydoyle
7/6/2017 - 11:55 AM

Canonical tagging in Contensis

Canonical tagging in Contensis

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
@using System.Collections
@using System.Collections.ObjectModel
@using System.Text.RegularExpressions;
@{
    if (CurrentNode.Data.Property_SynchronisationSourceMasterContentID >= 0)
    {
        int masterPageContentID = CurrentNode.Data.Property_SynchronisationSourceMasterContentID;
        Node masterPage = null;
        //If the master page content ID doesn't match the current page content ID then we are on a slave page
        if (masterPageContentID != CurrentNode.Data.Property_C_ID)
        {
            //Here we are returning the source page as a Node so we can use WebApi Methods
            IQuery masterPageQuery = Query.Where("Property_C_ID").IsEqualTo(masterPageContentID.ToString()).And("Property_CT_ID").IsEqualTo("0");
            ReadOnlyCollection<ContentNode> masterPages = new NodeFinder().Find(masterPageQuery);
            masterPage = masterPages[0];
            ((CMS_API.WebUI.Page.BasePage)HttpContext.Current.CurrentHandler).ContensisHeader.Text += "<meta rel='canonical' href='" + @masterPage.Path + "' />";
        }
        // If it's not a slave page, but the page has a query string, we need to set the canonical tag to point at the non-querystringed version. And yes, querystringed is a word.
        else if (Request.QueryString.Count > 0 && CurrentNode.Data.Property_SynchronisationSourceMasterContentID >= 0)
        {
            string currentUrl = CurrentNode.Path;
            var canonicalUrl = currentUrl.Split('?')[0];
            ((CMS_API.WebUI.Page.BasePage)HttpContext.Current.CurrentHandler).ContensisHeader.Text += "<meta rel='canonical' href='" + @canonicalUrl.ToString() + "' />";
        }   
    }
    
}