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));
}