SiteMapBase.GetActualCurrentNode returns null
Hello
The code below for getting the current page is returning null when used in Page_Load on a template masterpage:
var psn = SiteMapBase.GetActualCurrentNode();
Is this the wrong place to use this code?
Thanks
Ryan
A little more testing, and this only occurs on the homepage of the site, if I don't specify the page "/home" in the url address bar. Clicking on "Home" in the menu, and the code returns the PageNode. What is the solution here, to get the root node?
My quick fix:
01.
PageManager pageManager =
new
PageManager();
02.
var psn = SiteMapBase.GetActualCurrentNode();
03.
PageNode pgn1;
04.
if
(psn !=
null
)
05.
06.
pgn1 = PageManager.GetManager().GetPageNode(psn.Id);
07.
08.
else
09.
10.
PagesConfig pagesConfig = Config.Get<PagesConfig>();
11.
pgn1 = pageManager.GetPageNode(pagesConfig.HomePageId);
12.
Thanks, I had this same issue and this post helped.
I made a function for finding the Guid of the current page ...
01.
public
static
Guid GetCurrentPageId()
02.
03.
Guid pageId;
04.
PageSiteNode psn = SiteMapBase.GetActualCurrentNode();
05.
if
(psn ==
null
)
06.
07.
PagesConfig pagesConfig = Config.Get<PagesConfig>();
08.
pageId = pagesConfig.HomePageId;
09.
10.
else
11.
12.
pageId = psn.Id;
13.
14.
return
pageId;
15.