how to get current page nodealis in kentico 12 MVC

innovix solutions asked on April 4, 2019 05:26

Hi All,

Is there a way to get current page nodealis to query page data in K12 MVC development? Thanks in advance.

Recent Answers


Roman Hutnyk answered on April 4, 2019 11:44

If I'm not missing anything there is no document context available in MVC, so the app knows nothing about current page. You have to pass some page identifier (guid, alias, id, etc.) to the controller, so it knows which page it should work with.

Here is some info on getting pages: https://docs.kentico.com/k12/developing-websites/retrieving-content-in-mvc-applications/displaying-page-content

And here is recommended approach for getting page based on node alias: https://docs.kentico.com/k12/developing-websites/configuring-page-urls-on-mvc-sites

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 4, 2019 18:23

Depending on the routing this will vary, i've rigged up my routing to use the NodeAliasPath in most cases, so i do this:

GenericWidgetPage FoundNode = (GenericWidgetPage) DocumentQueryHelper.GetNodeByAliasPath(HttpContext.Request.Url.AbsolutePath, GenericWidgetPage.OBJECT_TYPE);
            if (FoundNode != null)
            {
                HttpContext.Kentico().PageBuilder().Initialize(FoundNode.DocumentID);
                SetContext(FoundNode.DocumentID);

                return View(FoundNode.Layout);
            }
            else
            {
                return HttpNotFound("Could not find page by that Url");
            }

I have a special SetContext in my BaseController that all my controllers reference to restore my "DocumentContext" type of thing:

public void SetContext(int DocumentID, string CultureCode = null, string SiteName = null)
        {            
            TreeNode DocumentNodeForClass = new DocumentQuery().WhereEquals("DocumentID", DocumentID).FirstOrDefault();
            TreeNode DocumentContext = null;
            if(DocumentNodeForClass != null)
            {
                CacheableDocumentQuery RepeaterQuery = new CacheableDocumentQuery(DocumentNodeForClass.ClassName);
                RepeaterQuery.WhereEquals("DocumentID", DocumentID);
                RepeaterQuery.CacheItemNameParts.Add("documentid|"+DocumentID);

                if (EnvironmentHelper.PreviewEnabled)
                {
                    RepeaterQuery.LatestVersion(true);
                    RepeaterQuery.Published(false);
                }
                else
                {
                    RepeaterQuery.PublishedVersion(true);
                }
                DocumentContext = RepeaterQuery.GetTypedResult().Items.FirstOrDefault();
            }
            if(DocumentContext != null)
            {
                ViewBag.CurrentDocument = DocumentContext;
                if(string.IsNullOrWhiteSpace(SiteName))
                {
                    SiteName = DocumentContext.NodeSiteName;
                }
                if(string.IsNullOrWhiteSpace(CultureCode))
                {
                    CultureCode = DocumentContext.DocumentCulture;
                }
            }
            if (!string.IsNullOrWhiteSpace(SiteName))
            {
                ViewBag.CurrentSite = CacheHelper.Cache<SiteInfo>(cs =>
                {
                    SiteInfo SiteObj = SiteInfoProvider.GetSiteInfo(SiteName);
                    if (cs.Cached)
                    {
                        cs.CacheDependency = CacheHelper.GetCacheDependency("cms.site|byid|" + SiteObj.SiteID);
                    }
                    return SiteObj;
                }, new CacheSettings(CacheHelper.CacheMinutes(SiteName), "GetSiteInfo", SiteName));
            }
            if(!string.IsNullOrWhiteSpace(CultureCode))
            {
                ViewBag.CurrentCulture = CacheHelper.Cache<CultureInfo>(cs =>
                {
                    CultureInfo CultureObj = CultureInfoProvider.GetCultureInfo(CultureCode);
                    if (cs.Cached)
                    {
                        cs.CacheDependency = CacheHelper.GetCacheDependency(CultureInfo.TYPEINFO.ObjectClassName+"|byid|" + CultureObj.CultureID);
                    }
                    return CultureObj;
                }, new CacheSettings(CacheHelper.CacheMinutes(SiteName), "GetCultureInfo", CultureCode)); ;
            }
        }

My DocumentHelper that gets the Node by the Path is this:

public static TreeNode GetNodeByAliasPath(string Path, string ClassName = null, string CultureCode = null)
        {
            return CacheHelper.Cache<TreeNode>(cs =>
            {
                List<string> CacheDependencies = new List<string>();
                TreeNode FoundNode = DocumentQueryHelper.RepeaterQuery(Path: Path, ClassNames: ClassName, CultureCode: CultureCode).GetTypedResult().Items.FirstOrDefault();
                if (FoundNode == null)
                {
                    // Check Url Aliases
                    var FoundNodeByAlias = DocumentAliasInfoProvider.GetDocumentAliasesWithNodesDataQuery().WhereEquals("AliasUrlPath", Path).Or().Where(string.Format("'{0}' like AliasWildCardRule", SqlHelper.EscapeQuotes(Path))).FirstOrDefault();
                    if (FoundNodeByAlias != null && FoundNodeByAlias.AliasNodeID > 0)
                    {
                        CacheDependencies.Add("cms.documentalias|all");
                        CacheDependencies.Add(string.Format("node|{0}|{1}", EnvironmentHelper.CurrentSiteName, Path));
                        FoundNode = DocumentQueryHelper.RepeaterQuery(NodeID: FoundNodeByAlias.AliasNodeID, ClassNames: ClassName, CultureCode: (!string.IsNullOrWhiteSpace(FoundNodeByAlias.AliasCulture) ? FoundNodeByAlias.AliasCulture : CultureCode)).GetTypedResult().Items.FirstOrDefault();
                    }
                }
                if (FoundNode != null)
                {
                    CacheDependencies.Add("documentid|" + FoundNode.DocumentID);
                }
                if (cs.Cached)
                {
                    cs.CacheDependency = CacheHelper.GetCacheDependency(CacheDependencies.ToArray());
                }
                return FoundNode;
            }, new CacheSettings(CacheHelper.CacheMinutes(EnvironmentHelper.CurrentSiteName), Path, ClassName, CultureCode));
        }
1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.